Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: "possible loss of precision", except not, code doesn't work, simple question

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default "possible loss of precision", except not, code doesn't work, simple question

    Hello,

    I'm trying to make my array as compact as possible. since a calculation will not go above 60, i used byte data type, but the compiler says 'possible loss of precision' and then doesn't run the array. Here is the code fragment:

    tarray=new byte[6][10];
    		for(byte y=0;y<6;y++){
    			for(byte x=0;x<10;x++){
    				tarray[y][x]=(y*10)+x;
    				System.out.println("tilearray "+y+","+x+": "+tarray[y][x]);
    			}
    		}

    The following very similar code has the same error, but outputs the value:

    byte six=6;
    byte ten=10;
    byte mul=six*ten;
    System.out.println(mul);

    So why doesn't my array output the information.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: "possible loss of precision", except not, code doesn't work, simple question

    Tell the compiler you know what you are doing by casting the int result to byte:
    int var = (byte) (int expression here);

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: "possible loss of precision", except not, code doesn't work, simple question

    thank you Norm, it's fixed

Similar Threads

  1. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  2. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM
  3. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  4. Arrays: question about "removing" items.
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2009, 04:09 AM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM