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

From: Grant Wagner (gwagner_at_agricoreunited.com)
Date: 07/29/04


Date: Thu, 29 Jul 2004 16:23:53 GMT

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......
    ... not the Java sense) is usually an 8 bit ... > 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, ... > i" both declare variables. ...
    (comp.lang.java.help)
  • Re: Separation of gui and business layer: Duplication of data necessary?
    ... > reference (except for primitive types), ... This semms to be true but the other things are "by reference". ... > using Java you have 650MB. ...
    (comp.object)
  • Re: Pass by reference / pass by value
    ... > - All parameters in Java are passed by value. ... > - Parameters may be either references or primitive types. ... you must pass a reference that ... > Chris Smith - Lead Software Developer/Technical Trainer ...
    (comp.lang.java.programmer)
  • Re: How java passes object references?
    ... Which everybody knows, it doesn't in Java. ... The reason I think this is a useful clarification is that when you got to the part about how passing by reference might work, it seems you went off track at least partly because you didn't understand the nature of the above. ... is a pointer pointing at the memory block. ... Assignments to local variables, or even to class members, do not allocate memory. ...
    (comp.lang.java.programmer)