0
Any reason why this dosent work?
x = "red"; System.out.println(String.x);
7 Réponses
+ 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) ;
+ 1
it's not similar to python..
u need to mention the datatype before initialising the variables
+ 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
+ 1
Np, happy to help! 😇
0
so with java you cant declare a variable without having a return type? is this the same with class?
0
thanks, im just experimenting ^_^
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.