*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unary operator" Watch "Unary operator" New topic
Author

Unary operator

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Unary operators,conversion rules please

char x1=+x;
int x1=++x;

How the previous conditions are working.Can anyone please explain all the unary conversion rules.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

The first one is the unary plus operator. I don't believe the statement as you have it written is legal -- as the result of a mathematical expression gets promoted to int. So you will need to cast it back to a char (assuming x was originally a char).

The second operation is the prefix increment operator. And that should be legal assuming that x was an int (or smaller datatype).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
char x1=+x;
int x1=++x;

In the above statment i am assuming that the data type of that x is int and you are adding that int with char including plus symbole. It will genrate a compile time error that

possible lose of precission

found int
requred character

and your second statment that is int x1=++x. Here ++ is the unary prefix operator. Here the whatever the value in the x that value will be first increse with 1 and assigned to that of x1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unary operator
 
Similar Threads
equal() and hashcode()..
WA #1.....word association
simple operator precedence question
diffrence between a++ and a=a+1
1.5 Integer with +=