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: java Conversion & Casting

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question java Conversion & Casting

    What will be the output when you compile and run the program? Explain your answers.

    class Example{
    public static void main(String args[]){
    byte b1=10,b2=20,b3;
    b3=b1+b2; //Line 1
    b3=b1+1; //Line 2
    b3=b1*2; //Line 3
    short s1=10,s2=20,s3;
    s3=s1+s2; //Line 4
    s3=s1+1; //Line 5
    s3=s*1; //Line 6
    int x1=10,x2=20,x3;
    x3=x1+x2; //Line 7
    x3=b1+b2; //Line 8
    x3=b1+1; //Line 9
    x3=b1*2; //Line 10
    x3=s1+s2; //Line 11
    x3=s1+1; //Line 12
    x3=s1*1; //Line 13
    }
    }

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

    Default Re: java Conversion & Casting

    What do you think?
    The code needs lots of print statements to have output. Try adding print statements so you can see what the values are for each statement and variable.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Conversion & Casting

    Please add System.out.println after each assignment ie
    b3=b1+b2; //Line 1
    System.out.println( " Value of b3 now "+b3);
    by the way I noticed following compilation error with your program ...did you get this ?
    incompatible types: possible lossy conversion from int to byte
    b3=b1+b2; //Line 1

Similar Threads

  1. Java help with String Input and Output and casting
    By jadenrocks12 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 2nd, 2014, 05:21 PM
  2. Casting and Data Type Conversion examples
    By tonu in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 3rd, 2014, 08:46 AM
  3. Object type casting in java
    By kinu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2013, 04:06 PM
  4. [SOLVED] Java casting/combining char
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 27th, 2012, 10:16 PM
  5. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM

Tags for this Thread