Re: newbie lost in strings......

From: Carl Smotricz (carl.smotricz_at_t-online.de)
Date: 07/30/04


Date: Fri, 30 Jul 2004 01:56:51 +0200

I'd like to take a crack at answering why byte goes to 127!

A byte (in the general, not the Java sense) is usually an 8 bit
quantity; the 8 bits can be thought of as representing the positive
integers from 0 (no bits set) to 255 (all 8 bits set).

Java, however, rules that all integral primitive types (byte, int and
long) shall hold *signed* values. This means one bit is used as a sign
bit, with the advantage that those (in the case of byte) 8 bits can now
hold negative numbers as well, but the magnitude is a bit less. Since
2's complement is used for the representation, the values actually go
from -128 to 127.

Hope that helps.

-Carl-

Grant Wagner wrote:
> Frances Del Rio wrote:
>
>
>>ok, I'm INCREDIBLY confused about the diff. types of variables/data, b/c
>>before only prog I had done was JavaScript (and a bit of Pearl/CGI..) of
>>course in java, unlike JavaScritp, you have to say WHAT TYPE of data a
>>variable is..
>>
>>in
>>http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
>>they list the diff. types of variables:
>>
>> // integers
>> byte largestByte = Byte.MAX_VALUE;
>> short largestShort = Short.MAX_VALUE;
>> int largestInteger = Integer.MAX_VALUE;
>> long largestLong = Long.MAX_VALUE;
>>
>> // real numbers
>> float largestFloat = Float.MAX_VALUE;
>> double largestDouble = Double.MAX_VALUE;
>>
>> // other primitive types
>> char aChar = 'S';
>> boolean aBoolean = true;
>>
>>1) you say below that int and integer are two differnt types (??)
>>("int and double are simple data types. Integer and String are
>>objects.") ok, so 'string' is not a type of data but an object?? man...
>>so when we declare
>>
>> String str = "I like learning Java";
>>
>> this is not a variable but an object?? confusing...
>
>
> When you do String s = "A string"; you're declaring a variable s that holds a
> reference to a String object containing the sequence of characters 'A', ' ',
> 's', 't', 'r', 'i', 'n', and 'g'.
>
> When you do int i = 0; you're declaring a variable i that holds the integer
> value 0.
>
> When you do Integer i = new Integer(0); you're declaring a variable i that
> holds a reference to an Integer object which holds the integer value 0.
>
> In each case you're declaring (and initializing) a variable.
>
> In some cases, the variable holds a reference to an object. That object
> contains some (or many values of different types - which can be other objects
> or primitives).
>
> In some cases, the variable is one of the primitive types (byte, char, int,
> long, double, float, boolean) and contains the actual value of that type.
>
>
>>(not to speack of byte largestByte, etc... can't even begin to figure
>>this out... short largestShort.. largestInteger, etc... what is this,
>>pls??? Largest?? largest in the universe???
>
>
> Byte is a class, MAX_VALUE is a static final variable that contains the
> largest value that a Byte object can possibly hold. The reason Java includes
> this is that in the future MAX_VALUE may change. If you hard-coded 127
> everywhere and suddenly MAX_VALUE went down to 63 (this is extremely unlikely
> to happen, it's more likely to go up, actually it's much more likely to never
> change), then all your code would break because a Byte can't hold a value
> larger then 63.
>
> When you write code that manipulates primitive types, sometimes you need to
> know the maximum or minimum values these primitives (and their corresponding
> classes: Byte, Integer, Long, Float, Double, etc) can hold. It's much better
> to ask the class what these limits are instead of assuming they are what they
> were in some previous version of Java. That's what you're doing when you write
> Byte.MAX_VALUE. You're asking the Byte class to return the value stored within
> itself called MAX_VALUE. This value is the maximum value that can be stored in
> a primitive byte, or an object created from the Byte class.
>
>
>>this stuff returns
>>
>>The largest byte value is 127
>>The largest short value is 32767
>>The largest integer value is 2147483647
>>The largest long value is 9223372036854775807
>>The largest float value is 3.40282e+38
>>The largest double value is 1.79769e+308
>>The character S is upper case.
>>
>>'largest byte value is 127' I mean how do we get to 127?? 32767?? ok,
>>very confused, thank you again for your help.. Frances
>
>
> They "get to" those values because someone wrote:
>
> public final class Byte extends Number implements Comparable {
>
> /**
> * A constant holding the maximum value a <code>byte</code> can
> * have, 2<sup>7</sup>-1.
> */
> public static final byte MAX_VALUE = 127;
>
> etc, etc
>
> The authors of Java have declared a variable inside the Byte class. It's
> public, so everyone can see it, it's static (which means you can use
> Byte.MAX_VALUE to see it), it's final (so someone can't do Byte.MAX_VALUE = 0;
> /* haha I messed with your head! */) and it's of the primitive type byte. It's
> assigned the value 127.
>
> I think you need to go back to the basics and understand the difference
> between a variable (which is just a symbolic name that points at or contains
> something) with the actual value that variable contains. "Integer ii" and "int
> i" both declare variables. You're telling Java ii will contain a reference to
> an Integer object, and i will contain an integer value.
>
> --
> Grant Wagner <gwagner@agricoreunited.com>
> comp.lang.javascript FAQ - http://jibbering.com/faq
>



Relevant Pages

  • Re: programming concepts > specific languages
    ... >actually changes the target of the reference passed to 'function2'. ... Java from a source that was not precise about terminology. ... if I have a class MyObject and write ... I thought primitive types in particular behave in the ...
    (comp.programming)
  • Re: newbie lost in strings......
    ... When you do int i = 0; you're declaring a variable i that holds the integer ... holds a reference to an Integer object which holds the integer value 0. ... In some cases, the variable is one of the primitive types (byte, char, int, ... The reason Java includes ...
    (comp.lang.java.help)
  • Re: Const Iterator?
    ... C++ you can declare a function as returning a const reference or pointer ... Java has no equivalent to that. ... Collections.unmodifiableList function does for Lists. ...
    (comp.lang.java.programmer)
  • Re: Generics are cool
    ... >> Tim Tyler wrote: ... >>> of Java. ... >>> never to have had primitive types in the first place. ... > that having everything be an object involves a performance hit is ...
    (comp.lang.java.programmer)
  • Re: How to check for already running program?
    ... I don't know about C++ but in Java you can't declare an object, ... onject *reference*. ...
    (comp.unix.programmer)