permaculture playing cards*
The moose likes Beginning Java and the fly likes Process of System.out.println()... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Process of System.out.println()..." Watch "Process of System.out.println()..." New topic
Author

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

Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
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

Joined: Jan 08, 2005
Posts: 375
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

Joined: Apr 21, 2005
Posts: 61
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

Joined: Jan 08, 2005
Posts: 375
Well "name[5]" returns an empty char " ", so that is what they're initialized to. Strings are "null" by default.
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
Ok..as name[5] indicates null character...How that output 195 came..Pls explain me in detail...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
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

Joined: Apr 21, 2005
Posts: 61
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Process of System.out.println()...