+ 2
when i write a simple program in eclipse it doesn't run plz tell me what's the error
class MyClass{ public static void main(String[ ] args) { int age ="22"; System.out.println(age); } }
9 Answers
+ 4
To expand a bit on Aquarius_TheJavaWolf's answer: the "" around your 22 told Java to interpret the "22" as a String object, rather than an int. But because you defined age as an int, the age variable can only take int values.
Java is a "strongly typed" language, which means that it doesn't usually try to interpret what type you meant. It's more likely to generate an error when it sees a type it doesn't expect, rather than changing the type it gets to match the type it needs.
This is especially important when you're dealing with numbers, as they can be represented in a lot of different ways:
String ageString = "22";
byte ageByte = 22;
short ageShort = 22;
int ageInt = 22;
long ageLong = 22L;
float ageFloat = 22f;
double ageDouble = 22.0;
Each of these data types allows you to do different things, and/or has different restrictions. (For example, byte takes up very little memory, but can only store numbers from -128 to 127.) And, as you can see, most of them use a different syntax for their definitions.
+ 2
ok so i found out your problem. when i tested out your code it gave me an error. However if you look at your int and age which is 22, you have " " around your number. theres no reason for that. heres what your code should look like:
public class Program
{
public static void main(String[] args) {
int age= 22;
System.out.println(age);
}
}
+ 1
glad i could help
+ 1
you don't need the quotation marks around the 22
+ 1
donot put " " on 22
0
thank you so much :) Aquarius
0
@An Owomoyela, that was well said
0
quite a good explanation thank you@ An owomoyela
0
ۧÙŰŻÙ۱Ù