aspose file tools*
The moose likes Beginning Java and the fly likes Why declaring a variable as string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why declaring a variable as string?" Watch "Why declaring a variable as string?" New topic
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: 18570
    
    8

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: 11320
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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: 38902
    
  23
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: 18570
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why declaring a variable as string?