0

Any reason why this dosent work?

x = "red"; System.out.println(String.x);

19th Jul 2017, 8:24 AM
D_Stark
D_Stark - avatar
7 odpowiedzi
+ 2
Think of yourself as a compiler. You have been given these lines of code- 1 x = "red"; 2 System.out.println(String.x); First question that should come to your mind should be x is not declared as variable but has been assigned to string literal "red". Ideally any variable should be declared before using/assigning it a value. Example: String x ="red"; Going to the second line it is print statement that will print a value of argument passed. x is not a instance of String class(syntactic incorrect). We cannot write String.x instead we can directly pass it as x. The correct code should be- String x = "red" ; System.out.println(x) ;
19th Jul 2017, 9:11 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
it's not similar to python.. u need to mention the datatype before initialising the variables
19th Jul 2017, 8:27 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
return type? first learn the lessons please then make queries here.. we cannot explain each and everything in so depth.. so please first finish up the lessons of the given courses and then if u face any problem then let us know.. for your given question..it should be in such a way String x="red"; System.out.println(x); //enclosed in the class and then inner enclosed in the void main
19th Jul 2017, 8:34 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Np, happy to help! 😇
19th Jul 2017, 9:23 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
0
so with java you cant declare a variable without having a return type? is this the same with class?
19th Jul 2017, 8:30 AM
D_Stark
D_Stark - avatar
0
thanks, im just experimenting ^_^
19th Jul 2017, 9:22 AM
D_Stark
D_Stark - avatar
0
@sami String< is a return value type i was wondering if i could set it inside the print parenthesis. its easier not to comment on a question then to answer a question you didnt want to answer in the first place. the whole point in Q&A's is to ask questions and get answers otherwise there would be no point in asking a question as you would already know the answer.
19th Jul 2017, 9:39 AM
D_Stark
D_Stark - avatar