Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why declaring a variable as string?

 
Dhivya rajagopal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, java takes everything as string, then why we declaring a variable as string

ie String s="java";



 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhivya rajagopal wrote:java takes everything as string


I don't understand this statement. Could you explain it?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Akondi
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50265
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic