Way to declare & initialize a String variable named "message" with a value of "Hello World" in Java?

Опубликовано: 24 Апрель 2023
на канале: SDET Automation Testing Interview Pro
812
20

JAVA : SELENIUM : Correct way to declare and initialize a String variable named "message" with a value of "Hello World" in Java?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : SELENIUM : Correct way to declare and initialize a String variable named "message" with a value of "Hello World" in Java?

a) String message = "Hello World";
b) message = "Hello World";
c) string message = new string("Hello World");
d) String message = new String("Hello World");

Answer: (a) String message = "Hello World";

Explanation:

Option (b) is incorrect because it is missing the type declaration.

Option (c) is incorrect because it is using the wrong class name and syntax to create a new instance of the String class.

Option (d) is incorrect because it is using the constructor of the String class to create a new instance, which is unnecessary since string literals are automatically converted to String objects.

Option (a) is the correct way to declare and initialize a String variable in Java, using the type declaration "String", the variable name "message", the assignment operator "=", and the value "Hello World" enclosed in double quotes.


Смотрите видео Way to declare & initialize a String variable named "message" with a value of "Hello World" in Java? онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь SDET Automation Testing Interview Pro 24 Апрель 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 812 раз и оно понравилось 20 людям.