A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Certification
»
Programmer Certification (SCJP/OCPJP)
Author
Problem in println
rock stone
Greenhorn
Joined: Jul 19, 2007
Posts: 3
posted
Jul 26, 2007 09:47:00
0
Hi
please check the code below, i'm unable to understand that how that 2nd println is producing an output of 38.
public class check { public static void main(String a[]) { System.out.println(4+" "+2);//prints 42 System.out.println(4+' '+2);//prints 38 } }
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
posted
Jul 26, 2007 10:54:00
0
Howdy Rock!
Welcome to the Ranch!
The second line adds an int + a char + an int.
This is no
String
concat. The char is converted to an int, and since the output is 38,
I guess that
char x = 38-4-2;
the x should be a char.
Yours,
Bu.
all events occur in real time
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
I like...
posted
Jul 26, 2007 11:50:00
0
Originally posted by Burkhard Hassel:
...I guess that
char x = 38-4-2;
the x should be a char...
Indeed, the space char has a
decimal value
of 32. So 4 + ' ' + 2 evaluates as 4 + 32 + 2.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award."
~Joe Strummer
sscce.org
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Problem in println
Similar Threads
increment post and pre, when used in println
Increment & Assignment
Calendar Class (Wrong month! )
Array Question
String.split() method functionality
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter