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 4 of 4

Thread: Integer type conversions

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

    Default Integer type conversions

    Hi everybody, I have a practice task that I can't solve and I need help.

    public class Solution {
        public static void main(String[] args) {
            short number = 9;
            char zero = '0';
            int nine = (char) (zero + number);
            System.out.println(nine);
        }
    }

    Here are the rules:
    1. The program should display text on the screen.
    2. Don't change the screen output command. You can only add cast operators to it.
    3. The main() method must contain a short variable number.
    4. The main() method must contain a char variable zero.
    5. The main() method must contain an int variable nine.
    6. Don't change the initial value of the variables during initialization. You can only add cast operators.
    7. The program should display the number 9.

    If I change the variable nine to char, the assignment is incorrect. Also, I don't get why this code it's not working when zero plus number is equal 57 and the decimal 57 is equal to the character 9.

    If someone knows why this is happening I would also like explaining if possible. I'm not looking just for the answer. Thanks!

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Integer type conversions

    You might be casting it to a char but it's being assigned to an int.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Integer type conversions

    Hey, Jim! Thank you for replying.

    I figure that char + whole number is equal to char, that's why zero and number is equal to 57 because zero(char) + 9(short) is equal to char 9 which is 57 (decimal). If I change int nine = ((short) zero + number);, I still get the same answer 57. Why does this happens If I added short and short.

    Thanks.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Integer type conversions

    When you assign a char to an int it becomes an int. So int a = (char)'0' is no different than int a = '0'. Same is true with short or byte since when you assign to an int it will simply be assigned with any sign extension requirements. Casting is used when you want to assign something to a type where automatic conversion won't take place (the compiler won't do it for you). Like int a = 3.6 won't work. You need to cast 3.6 to an int to assign it to an int type. So when you add to shorts together and assign to an int, it will do the conversion for you.

    So in the following line:

    int nine = (char)(zero + number) is the same as
    int nine = (zero + number).

    Regards,
    Jim

  5. The Following User Says Thank You to jim829 For This Useful Post:

    martinevtimov (October 19th, 2018)

Similar Threads

  1. Return type of converting integer to string for linked list
    By jcfor3ver in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2013, 07:37 PM
  2. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  3. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  4. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  5. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM