Welcome to Dream.In.Code
Become a Java Expert!

Join 150,184 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,106 people online right now. Registration is fast and FREE... Join Now!




char half

 
Reply to this topicStart new topic

char half

IMU
23 Sep, 2008 - 11:51 PM
Post #1

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 38

public class CharDemo
{
public static void main(String [] args)
{
char a = ‘A’;
char b = (char) (a + 1);
System.out.println(a + cool.gif;
System.out.println(“a + b is “ + a + cool.gif;
int x = 75;
char y = (char) x;
char half = ‘\u00AB’;
System.out.println(“y is “ + y + “ and half is “ + half);

}
}

can any one explain me how this char half='\u00AB' will become1/2 @ output..

how do we get 1/2 in output...
User is offlineProfile CardPM
+Quote Post

thenovices
RE: Char Half
24 Sep, 2008 - 05:10 PM
Post #2

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
the \u thingy shows that the next 4 characters represent a unicode character. I don't know where you got 00AB for 1/2, I found 00BD at this website: here
User is offlineProfile CardPM
+Quote Post

pbl
RE: Char Half
24 Sep, 2008 - 05:21 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
I was puzzzled by your code:

Actually it does not compile:

CODE

public class CharDemo
{
    public static void main(String [] args)
    {
        char half = '\u00AB';
        char a = (char) 0xBD;
        System.out.println(" half: " + half + " a: " + a);
        half = '\u00BD';
        System.out.println(" half: " + half + " a: " + a);
    }
}


CODE

half: « a: ½
half: ½ a: ½



This proves that Unicode \u00AB is "«"
And \u00BD as Ascii 0xBD is "½"
User is offlineProfile CardPM
+Quote Post

IMU
RE: Char Half
24 Sep, 2008 - 09:03 PM
Post #4

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 38

guys when i execute the program using'\u00AB' i am getting output as 1/2..

how is it possible..

//this is the output i got for the program.
CODE
J:\vinay\javaprg\lab1_2>javac chardemo.java

J:\vinay\javaprg\lab1_2>java chardemo
131
a + b is AB
y is K and half is ½


CODE
public class chardemo
{
public static void main(String [] args)
{
char a = 'A';
char b = (char) (a + 1);
System.out.println(a + b);
System.out.println("a + b is " + a + b);
int x = 75;
char y = (char) x;
char half = '\u00AB';
System.out.println("y is " + y + " and half is "+ half);
}
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Char Half
24 Sep, 2008 - 09:10 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
beleive you
that is what I have on my computer

131
a + b is AB
y is K and half is «

Windows XP
Regional settings: US

What are your Regional Settings ?
User is offlineProfile CardPM
+Quote Post

IMU
RE: Char Half
24 Sep, 2008 - 09:50 PM
Post #6

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 38

same settings ..but my output is what i said


it is in the book java in 60 min a day..and i am working on the example.
User is offlineProfile CardPM
+Quote Post

reyben_89
RE: Char Half
6 Dec, 2008 - 11:33 PM
Post #7

New D.I.C Head
*

Joined: 30 Nov, 2008
Posts: 39



Thanked: 1 times
My Contributions
QUOTE(IMU @ 24 Sep, 2008 - 09:50 PM) *

same settings ..but my output is what i said


it is in the book java in 60 min a day..and i am working on the example.


based in my experience integer value has a corresponding value when w cast it in char
site this sample

CODE

int x = 65;
char y = (char) x;
char half = '\u00AB';
System.out.println("y is " + y + " and half is " + half);


CODE

y is A and half is ½


but if we change the value of x that we declare as an integer we notice that the value of y change
let see by this sample
CODE

int x = 66;
char y = (char) x;
char half = '\u00AB';
System.out.println("y is " + y + " and half is " + half);

now take a closer look on the on the result
CODE

y is B and half is ½


take a closer look at y is B;
before it is y is A;

This post has been edited by reyben_89: 6 Dec, 2008 - 11:41 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:01AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month