Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Process of System.out.println()...

 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for test on Strings in java i wrote a Program, the output of the program simply surprised me .. and my Program is:


How 195 c is came as output.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like your adding the ASCII values of the chars.

a = 97
b = 98
.
z = 122

So a + b = 195.

You get the "c" because of string concatenation. So if you do it for the other chars, you will the char output (such as "abc"), as opposed to the ASCII addition.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank YOU...Just see my code once again;

char[] name=new char[20];
name[0]='a';
name[1]='b';
name[2]='c';
System.out.println(name[1]+name[5]+name[0]+" "+name[2]);


I used name[5].. which is not intialized..Is strings automatically initialized to default.if Yes to what value they initialized..
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well "name[5]" returns an empty char " ", so that is what they're initialized to. Strings are "null" by default.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..as name[5] indicates null character...How that output 195 came..Pls explain me in detail...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramakrishna Nalla:
I used name[5].. which is not intialized..Is strings automatically initialized to default.if Yes to what value they initialized..


name[5] is not a String, it's a char. char's are initialized to 0 (== '\000') as default value.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramakrishna Nalla:
Ok..as name[5] indicates null character...How that output 195 came..Pls explain me in detail...


I have already explained it, see my previous posts. It is the addition of the ASCII values of the two chars: B(98) + A(97). I cannot see what it is that you find difficult to understand.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand that one... Riaz. But I want to Know the details of Println() method. How this method treats each paratmeter..... When.will string concatenation occurs or when it will simply put char output.
println()..

Just Breifly Pls
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with println. It's just how the expression

name[1]+name[5]+name[0]+" "+name[2]

is evaluated. Expressions are evaluated from left to right. "name[1] + name[5] + name[0]" is adding three numbers together. When you add a char and a char, you get an int containing the sum of the char values. So 97 + 0 + 98 is 195.

So then we can simplify to 195 + " " + name[2]. Adding a number and a String converts the number to a printable representation, and then catenates it with the String, so you get "195 " from the first two terms. Likewise, adding a String and a char converts the char to a String containing that one char, and then catenates them, giving, in this case, "195 c". No surprises.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic