HOME       >>       Programming

Learning Java


ultranet

Hey, I'm new and I want to leanr Java, Who can Help me?


xpress

Hey, I'm new and I want to leanr Java, Who can Help me?

Well, Many people are here to help you. But first let us know what you want. A small suggestion,
posting requests like these in other people's threads may not be good for you...I think.

OK. I will help you. What information do you want? Do you have any doubts? or you want
information like where to start etc..? Feel free to ask anything. But be clear....

one_nikhil

Hello xpress,Please help me. I am learning java and getting this error in this simple program :-class test { public static void main(String[] args) { byte b = 5; b = b * 2; System.out.println("Answer is "+b+"."); }}Please give me the correct code for this. Thank you in advance.


xpress

byte b = 5;

b = b * 2;

System.out.println("Answer is "+b+".");

}

The correct program is,

 

class test {

 

public static void main(String[] args){

 

byte b=5;

b=(byte)(b*2);

 

System.out.println("Answer is "+b+".");

}

}

 

Why did we add byte at b=b*2 ?

Because, java doesn't allow direct arithmetic operations with byte and short(to save from potential loss of precision). Java automatically promotes byte into int here. That is, the result of b*2 will be treated as int rather than byte by java compiler. So we are converting it back to byte by using that keyword before the expression.

 

A small suggestion. Use upper case for first letter in class name(use class Test instead of class test). It is a common convention in java.


snowisawsome

I recommend Java For Dummies if you would like to learn Java, it is an excellent reasource. I taught myself Java using that book inconjunction with Java 2 For Dummies, you can also find tutorials online at http://www.oracle.com/technetwork/java/index.html but I find them more confuzing as a first time learning tool.



VIEW ORIGINAL REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.