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

Thread: Can an int array contain a short or byte?

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

    Default Can an int array contain a short or byte?

    I was asked the following True/False question on a test.

    An array declared as an int can contain elements of different primitive types.
    True or False?

    I hated the question the moment I read it.

    For some background, the textbook said the following:

    "All values stored in an array have the same type (or are at least compatible)."

    The textbook also said:

    "A value stored in an array is sometimes called an array element...."

    What do you think? Is it true or false?
    "An array declared as an int can contain elements of different primitive types."


    Is 00001111 "contained in" 0000000000001111 if the array is composed of the latter "type" of data?

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

    Default Re: Can an int array contain a short or byte?

    It's true. A byte is 8 bits, a short is 16 and both will fit into an int which is 32 bits. And even if the byte or short is negative, the sign should extend when assigned to an int.

    Regards,
    Jim

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

    snyderthing (October 13th, 2018)

  4. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can an int array contain a short or byte?

    Thank you.

    I answered "True" and my answer was marked wrong. The "feedback" was:

    "The type of the array is the type for each element."

    I tried to argue my case, but the instructor just dogmatically asserted that an int array only contains ints.
    I saw that the question could be viewed as both true or false.

    Does anyone else think that the question should have been answered true? It was obvious to me that an int array couldn't contain a String, but not obvious that an int array couldn't contain a short or byte.

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

    Default Re: Can an int array contain a short or byte?

    If you are talking about whether an int array can contain other "types" the answer is no. An int array may only contain a type int. But you asked if an int array can contain a short or a byte and the answer is yes. A byte is 8 bits and a short is 16 bits. So both will easily fit inside an int.

    public class ByteToInt {
     
       public static void main(String[] args) {
    	  byte[] bytes = {20,-1,23,99,122,-45, -111, 101};
    	  int len = bytes.length;
    	  int[] ints = new int[len];
    	  for (int i = 0; i < len;i++) {
    		 ints[i] = bytes[i]; 
    	  }
     
    	  for (int i = 0; i < len; i++) {
    		 System.out.printf("%4d %4d%n", ints[i], bytes[i]);
    	  }
       }
     
    }

    Regards,
    Jim
    Last edited by jim829; October 13th, 2018 at 07:29 PM.

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

    snyderthing (October 13th, 2018)

  7. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can an int array contain a short or byte?

    Thanks, Jim.

    The most important question for me is the test question and whether it is actually a true or false question:

    An array declared as an int can contain elements of different primitive types.
    True or False?

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

    Default Re: Can an int array contain a short or byte?

    If it is stated like that I would say false. For example, an int array cannot contain a double because their internal representation is different and when you assign a double to an int you would loose the fractional part. And when you assign a byte or a short to an int you don't loose precision but the value becomes an int.

    Regards,
    Jim

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

    snyderthing (October 14th, 2018)

  10. #7
    Junior Member
    Join Date
    Oct 2018
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can an int array contain a short or byte?

    Thanks again, Jim.

    If this is false, "An array declared as an int can contain elements of different primitive types," then an array declared as an int CANNOT contain elements of type byte or short. Isn't that what that means? Bytes and shorts are different primitive types, are they not?

    What starts to happen is that the definitions of the words uses become the battleground for what makes the question true or false, don't you agree? Is a byte a different primitive type from an int in the exact same way as a String? Is the phrase "different primitive type" going to mean one thing in one situation and another thing in another situation?

    When a byte or short becomes an int, is not the byte or short still "contained" within the int?
    Is 00001111 still "contained in" 0000000000001111, etc?

    It seems impossible to teach that an int array CANNOT contain a byte or short. But, then again, is "contain" a word that makes the true/false statement ambiguous?

    Just the fact that you have answered True and False to the same true/false question seems to indicate that there is ambiguity.

    To be clear, the issue is definitely not whether anything other than a byte or short could be contained. I believe that I completely understand losing precision.

    If the question is about the value, should it then refer to actual values, such as whether 2e^31 could be an int as opposed to one less than 2e^31?

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

    Default Re: Can an int array contain a short or byte?

    The confusion arises from the term byte. A byte is also a computer term and not just a java type. So in that regard an int may contain a byte of information. Similarly but not as common, a short can refer to half an integer depending on the machine architecture. So if ints are 32 bits in length, then a short would be 16 bits. But if we are simply talking about java types then an int array may only contain values of type int and not of any other type.

    Regards,
    Jim

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

    snyderthing (October 14th, 2018)

  13. #9
    Junior Member
    Join Date
    Oct 2018
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can an int array contain a short or byte?

    I really do appreciate your responses, Jim. I can't thank you enough. If I haven't exhausted your patience, I'd like to share what my textbook taught and what the quiz question should really be taking into account. The following three quotations are what I was taught throughout the text (among other things, of course).

    "There are eight primitive data types: four subsets of integers, two subsets of floating point numbers, a character data type, and a boolean data type."

    "There are four integer data types (byte, short, int, and long)."

    "All values stored in an array have the same type (or are at least compatible)."

    ___

    Is there only one way to answer the true/false question if one has in mind the short and byte? Or would you say that there is ambiguity when thinking about bytes and shorts—about "compatible" types?
    "An array declared as an int can contain elements of different primitive types. T/F?

Similar Threads

  1. Can an int array contain a short or byte?
    By snyderthing in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 12th, 2018, 01:37 PM
  2. [SOLVED] [Asking] Convert String to Byte Array
    By ardisamudra in forum Java Theory & Questions
    Replies: 8
    Last Post: October 30th, 2012, 09:58 AM
  3. [SOLVED] Adding two byte/ short variables
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2012, 04:31 AM
  4. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM
  5. Convert Vector to Byte Array
    By perlWhite in forum Java Theory & Questions
    Replies: 0
    Last Post: August 25th, 2009, 05:45 AM