| Author |
Why declaring a variable as string?
|
Dhivya rajagopal
Ranch Hand
Joined: Dec 15, 2010
Posts: 42
|
|
Hello, java takes everything as string, then why we declaring a variable as string
ie String s="java";
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Dhivya rajagopal wrote:java takes everything as string
I don't understand this statement. Could you explain it?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
Java most certainly does NOT take everything as a string.
Arguments passed in from the command line are strings, and passed into a String array. However, once you are in a method, you can declare a variable of any type:
int a = 7;
Float f = new Float(2.7);
etc.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Ganesh Akondi
Greenhorn
Joined: Jul 13, 2011
Posts: 6
|
|
Please reframe the question divya.
As Fred said, Arguments passed in from the command line are strings, and passed into a String array (public static void main(String args[]))
We have different types of data types you can use for certain operations.
if you want to add two numbers that should be 2+3 not "2"+"3" right ??
(even though java has capability of conversion, for the time being like Integer.parseInt("2"))
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
You can of course write "2"+"3"
campbell@campbellsComputer:~/java> java TwoPlusThreeDemo 2 3
Try it and see
But you won't get very far with "2"-"3"
|
 |
Dhivya rajagopal
Ranch Hand
Joined: Dec 15, 2010
Posts: 42
|
|
Hello, while getting input at the console window, we used to write
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(br.readLine);
The above is used to get an integer value. My idea is as java takes everything as string, we are converting it to integer and getting a value.
If i am wrong, Let me know. Can you please explain it.
Thank you in advance.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Aha. So when you said "Java takes everything as string" you were really just thinking of BufferedReader and its ability to read lines from a file into String variables. There's a long way between "Java" and "BufferedReader"; you really should try to be a little bit more precise.
|
 |
 |
|
|
subject: Why declaring a variable as string?
|
|
|