Java: byte literals and short literals

Discussion in 'Java' started by John Goche, Jan 15, 2006.

  1. John Goche

    John Goche Guest

    Hello,

    It appears to me that a way of specifying byte literals and short
    literals has been left out of the Java language (unlike int literals
    which need no special syntax and long literals which can be
    specified by appending an L suffix to the number
    being represented).

    Anyone know how come these have been left out (I guess
    it must be because I can always cast an int literal to a
    short or byte).

    Also, quite a bummer that there are no signed ints and longs.
    When reading 16-bit unsigned values and 32-bit values from
    the network I am being forced to use ints and longs thus
    wasting a few bytes here and there, not that it matters
    much I suppose.

    Regards,

    JG
    John Goche, Jan 15, 2006
    #1
    1. Advertising

  2. "John Goche" <> writes:

    > Anyone know how come these have been left out (I guess
    > it must be because I can always cast an int literal to a
    > short or byte).


    All arithmetical expressions have type int, long, float, or double.
    I suppose they didn't think it worthwhile to complicate matters any
    further.
    Torkel Franzen, Jan 15, 2006
    #2
    1. Advertising

  3. John Goche

    Roedy Green Guest

    On 15 Jan 2006 11:24:17 -0800, "John Goche" <>
    wrote, quoted or indirectly quoted someone who said :

    >Also, quite a bummer that there are no signed ints and longs.


    you mean unsigned. For notes on kludging them see
    http://mindprod.com/jgloss/unsigned.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 15, 2006
    #3
  4. John Goche

    Roedy Green Guest

    On 15 Jan 2006 11:24:17 -0800, "John Goche" <>
    wrote, quoted or indirectly quoted someone who said :

    >Anyone know how come these have been left out (I guess
    >it must be because I can always cast an int literal to a
    >short or byte).


    You don't need them. If you write

    byte x = 3;

    You don't need a cast.

    If you write byte x = x + 3;

    the addition will promote x and 3 to int anyway, so there is no point
    in byte literals.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 15, 2006
    #4
  5. Roedy Green wrote:
    > If you write byte x = x + 3;
    >
    > the addition will promote x and 3 to int anyway, so there is no point
    > in byte literals.


    As well as the missing cast, you are using x before it is defined there.

    As an obscurity, you can write:

    byte x = 3;
    x += 1000;

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 15, 2006
    #5
  6. John Goche

    Roedy Green Guest

    On Sun, 15 Jan 2006 23:02:14 +0000, Thomas Hawtin
    <> wrote, quoted or indirectly quoted someone
    who said :

    >As well as the missing cast, you are using x before it is defined there.
    >
    >As an obscurity, you can write:
    >
    > byte x = 3;
    > x += 1000;

    byte x ....


    x = (byte)( x + 100 );

    needs a cast

    but

    x += 100;

    does not
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 16, 2006
    #6
  7. John Goche

    Chris Uppal Guest

    Re: byte literals and short literals

    John Goche wrote:

    > Anyone know how come these have been left out (I guess
    > it must be because I can always cast an int literal to a
    > short or byte).


    Perhaps because an expression like
    (byte)23
    is, in effect, a byte-valued literal.


    > Also, quite a bummer that there are no signed ints and longs.


    Bothers me too, sometimes. But that's how it goes...

    -- chris
    Chris Uppal, Jan 16, 2006
    #7
  8. John Goche

    Roedy Green Guest

    Re: byte literals and short literals

    On Mon, 16 Jan 2006 11:17:51 -0000, "Chris Uppal"
    <-THIS.org> wrote, quoted or indirectly
    quoted someone who said :

    >Perhaps because an expression like
    > (byte)23
    >is, in effect, a byte-valued literal.


    consider this SSCCE::

    package com.mindprod.example;

    /**
    * Test unsigned byte calculation
    *
    * @author Roedy Green
    */
    public class TestUnsignedByte
    {
    /**
    * Test unsigned byte calculation
    *
    * @param args
    * not used
    */
    public static void main ( String[] args )
    {
    // 0xca is a negative number considered as a signed byte.
    byte b = (byte)0xca;

    // false, b sign extends, 0xca does not
    System.out.println( b == 0xca );

    // true, b sign extends, 0xca sign extends
    System.out.println( b == (byte) 0xca );

    // true, b chopped, 0xca does not sign extend
    System.out.println( (b & 0xff) == 0xca );

    // false, b chopped, 0xca sign extends.
    System.out.println( (b & 0xff) == (byte) 0xca );
    }
    }





    What prints? scroll down after you have decided what it should be:
    For more detail see http://mindprod.com/jgloss/unsigned.html
    It explains why you get he various answers you do.































    false
    true
    true
    false
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 16, 2006
    #8
  9. John Goche

    mert Guest

    Hi

    There are several reasons for your questions. Nevertheless, I want t
    emphasize the most significant reason

    The reason : Because, they are both limited primitive types. Yo
    should be careful that they are limited with 8 and 16 bits which i
    small enough for programmers. However, float and long primitiv
    types are big enough for programmers. If a programmer always use lon
    or float although he or she deal with small numbers or not larg
    enough for them, it can allocate more memory size needlesly which ca
    have potential to reduce the computer's performance

    So, you should generally use int or double which can be beneficial fo
    you I guess
    Best wishes. :
    http://www.devplug.com
    mert, Jan 17, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. carmen
    Replies:
    4
    Views:
    30,669
    ersecchio
    Jan 12, 2010
  2. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    540
    Keith Thompson
    Jan 11, 2007
  3. Replies:
    4
    Views:
    793
    Kaz Kylheku
    Oct 17, 2006
  4. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    652
    Eric Sosman
    Mar 5, 2008
  5. Andre
    Replies:
    5
    Views:
    512
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page