• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

numbers, Strings and + operator

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all:
Please help me understand this:
Given the code
class Foo {
static byte a;
static short b;
static char c;
static int d;
static long e;
static String s;
public static void main(String[] args) {
System.out.println(a+b+c+d+e+s);
}
}
on jdk 1.4 the line (String comes last)
System.out.println(a+b+c+d+e+s);
prints: 0null
and the line (String comes first)
System.out.println(s+a+b+c+d+e);
prints: null00
Why two ceros?
Also, why is i use local variables (initialized variables) the operator behaives different?
String a = "String";
int b = 3;
int c = 7;
System.out.println(a + b + c);
prints: String37
Thanks.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first question,
When you put string variable at the first, it should print null followed by 4 zeros not two because all the rest of variables are concatenated and treated as string.
for the second question, I dont quite follow what you want to know. It prints string and the rest as string.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlos
It's not to do with the local variable the thing is that when you say a+b+c you convert all the operands to String first and they are concatnated and printed. When you do c+b+a you first add the two numbers then concatnate the result to the String.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the line (String comes first)
System.out.println(s+a+b+c+d+e);
prints: null00

Actually it prints:

null00
00

The default char value causes a new line to print.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think char default causes new line.
It's just a unicode null and should be ignored when concatenated.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints
null00 00
thus it is not ignored when concatenated but when printed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic