Home > Archive > Java Help > July 2004 > newbie lost in strings......
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
newbie lost in strings......
|
|
| Frances Del Rio 2004-07-21, 3:59 pm |
| I never had a hard time w/strings w/JavaScript.. I don't understand why
I find this so hard in Java.. am tyring strictly to understand the
indexOf method for now..
from java.sun API:
indexOf(int ch)
Returns the index within this string of the first occurrence of the
specified character.
PLEASE: what is 'int'? do I put a number in place of 'int'?? or do I
put 'int'? (I was assuming you put a number there, but it's not working
for me I'm not sure anymore..)
I have done:
1) String str = new String("I like learning Java");
String str2= str.indexOf(2 k);
String str2= str.indexOf(2 "k");
String str2= str.indexOf(int k);
String str2= str.indexOf(k);
this worked fine:
class search {
public static void main (String args[]) {
String str = new String("I like learning Java");
if (str.indexOf("k") != -1) {
System.out.println("that's it...");
}
}
}
so why won't this work?
class search2 {
public static void main (String args[]) {
String str = new String("I like learning Java");
String str2 = str.indexOf("k");
// also tried
// String str2= (str.indexOf("k"));
// also
// String str2 = new String(str.indexOf("k"));
System.out.println(str2);
}
}
would appreciate any help/suggestions... thank you..
Frances Del rio
| |
|
| In article <2m7i4cFjqii9U1@uni-berlin.de>, fdr58@yahoo.com enlightened
us with...
> I never had a hard time w/strings w/JavaScript.. I don't understand why
> I find this so hard in Java..
Probably because java is a LOT more complicated than javascript. *g*
> am tyring strictly to understand the
> indexOf method for now..
>
> from java.sun API:
> indexOf(int ch)
> Returns the index within this string of the first occurrence of the
> specified character.
>
>
> PLEASE: what is 'int'?
An integer. Note that all single characters are represented by integers.
;)
Non-printable characters are also represented by integers, so you could
look for things that have no representation in type (line feeds, for
example).
You would know this if you'd done any C programming. Javascript doesn't
really go into such details.
String s = "godiieis12ofi";
int i;
i = s.indexOf('o');
Note the single quotes denoting a character. Double-quotes are for
strings. If you use double-quotes, you'll get an error. Again, this is
different than javascript, which couldn't care less what kinds of quotes
you use.
HTH
--
--
~kaeli~
Does the name Pavlov ring a bell?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
| Madhur Ahuja 2004-07-21, 3:59 pm |
|
"Frances Del Rio" <fdr58@yahoo.com> wrote in message
news:2m7i4cFjqii9U1@uni-berlin.de...
> I never had a hard time w/strings w/JavaScript.. I don't understand why
> I find this so hard in Java.. am tyring strictly to understand the
> indexOf method for now..
>
> from java.sun API:
> indexOf(int ch)
> Returns the index within this string of the first occurrence of the
> specified character.
>
>
> PLEASE: what is 'int'? do I put a number in place of 'int'?? or do I
> put 'int'? (I was assuming you put a number there, but it's not working
> for me I'm not sure anymore..)
>
> I have done:
>
> 1) String str = new String("I like learning Java");
> String str2= str.indexOf(2 k);
> String str2= str.indexOf(2 "k");
> String str2= str.indexOf(int k);
> String str2= str.indexOf(k);
>
>
> this worked fine:
> class search {
> public static void main (String args[]) {
> String str = new String("I like learning Java");
> if (str.indexOf("k") != -1) {
> System.out.println("that's it...");
> }
> }
> }
> so why won't this work?
>
> class search2 {
> public static void main (String args[]) {
> String str = new String("I like learning Java");
> String str2 = str.indexOf("k");
> // also tried
> // String str2= (str.indexOf("k"));
> // also
> // String str2 = new String(str.indexOf("k"));
>
> System.out.println(str2);
> }
> }
>
> would appreciate any help/suggestions... thank you..
>
> Frances Del rio
>
>
>
>
>
>
>
>
Hello
indexOf method return a integer and not a String. You cannot assign the
return value to the String object.
See Java Platform API Specification on http://java.sun.com.
--
Winners dont do different things, they do things differently.
Madhur Ahuja
India
Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
| |
| Madhur Ahuja 2004-07-21, 3:59 pm |
|
"Frances Del Rio" <fdr58@yahoo.com> wrote in message
news:2m7i4cFjqii9U1@uni-berlin.de...
> I never had a hard time w/strings w/JavaScript.. I don't understand why
> I find this so hard in Java.. am tyring strictly to understand the
> indexOf method for now..
>
> from java.sun API:
> indexOf(int ch)
> Returns the index within this string of the first occurrence of the
> specified character.
>
>
> PLEASE: what is 'int'? do I put a number in place of 'int'?? or do I
> put 'int'? (I was assuming you put a number there, but it's not working
> for me I'm not sure anymore..)
>
> I have done:
>
> 1) String str = new String("I like learning Java");
> String str2= str.indexOf(2 k);
> String str2= str.indexOf(2 "k");
> String str2= str.indexOf(int k);
> String str2= str.indexOf(k);
>
>
> this worked fine:
> class search {
> public static void main (String args[]) {
> String str = new String("I like learning Java");
> if (str.indexOf("k") != -1) {
> System.out.println("that's it...");
> }
> }
> }
> so why won't this work?
>
> class search2 {
> public static void main (String args[]) {
> String str = new String("I like learning Java");
> String str2 = str.indexOf("k");
> // also tried
> // String str2= (str.indexOf("k"));
> // also
> // String str2 = new String(str.indexOf("k"));
>
> System.out.println(str2);
> }
> }
>
> would appreciate any help/suggestions... thank you..
>
> Frances Del rio
>
>
>
>
>
>
>
>
Hello
indexOf method return a integer and not a String. You cannot assign the
return value to the String object.
See Java Platform API Specification on http://java.sun.com.
--
Winners dont do different things, they do things differently.
Madhur Ahuja
India
Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
| |
| Frances Del Rio 2004-07-21, 3:59 pm |
| kaeli wrote:
> In article <2m7i4cFjqii9U1@uni-berlin.de>, fdr58@yahoo.com enlightened
> us with...
>
>
>
> Probably because java is a LOT more complicated than javascript. *g*
>
>
>
>
> An integer. Note that all single characters are represented by integers.
> ;)
> Non-printable characters are also represented by integers, so you could
> look for things that have no representation in type (line feeds, for
> example).
> You would know this if you'd done any C programming. Javascript doesn't
> really go into such details.
>
> String s = "godiieis12ofi";
> int i;
> i = s.indexOf('o');
>
> Note the single quotes denoting a character. Double-quotes are for
> strings. If you use double-quotes, you'll get an error. Again, this is
> different than javascript, which couldn't care less what kinds of quotes
> you use.
thank you very much.. this worked.. my code now:
String s = "I like learning Java";
int i;
i = s.indexOf('k');
System.out.println(i);
// returns "4" (w/o the quotes..)
what I still wd like to know is:
int i; // don't understand why this is not written:
int = i
also, I thought a new string always had to be declared like this:
String s = new String("I like learning Java"); // what is the diff.
// betw. this (above) line and
String s = "I like learning Java"; // ??
no, I have never done any C progamming. I have only done JavaScript and
some CGI/Perl.. speaking of "g", pls what is "g." in this line??
(from java.sun tut..)
g.drawString("Hello world!", 50, 25);
what does "g." mean? (and what is diff. betw. drawString and
System.out.println()?
(also, what is 50, 25)??
ok, lots of questions (yes, have many questions..) thank you very much
for your help.... Frances
| |
|
| In article <2m7t12FjgssmU1@uni-berlin.de>, fdr58@yahoo.com enlightened
us with...
> thank you very much.. this worked.. my code now:
>
> String s = "I like learning Java";
> int i;
> i = s.indexOf('k');
> System.out.println(i);
> // returns "4" (w/o the quotes..)
>
> what I still wd like to know is:
>
> int i; // don't understand why this is not written:
Java requires variables to be declared. Javascript does not.
I could also have written
int i = s.indexOf('k');
>
> int = i
That statement makes no sense. 'int' is a datatype. 'i' is a variable.
Similarly:
Integer i = new Integer(0);
String s = new String("hello world");
int i = 0;
double i = 0.00;
You would not write
String = s;
[int and double are simple data types. Integer and String are objects.]
In javascript, you *can* declare variables, but you don't have to unless
there's scope issues:
var i = 0;
var a = new Array();
you would not write
var = i;
In VB:
Dim i as Integer
Dim s as String
>
> also, I thought a new string always had to be declared like this:
>
> String s = new String("I like learning Java"); // what is the diff.
> // betw. this (above) line and
> String s = "I like learning Java"; // ??
What's the difference? Not much. Probably memory allocation or
something. I honestly don't know and have never had to use "new String"
type syntax.
>
> no, I have never done any C progamming. I have only done JavaScript and
> some CGI/Perl.. speaking of "g", pls what is "g." in this line??
> (from java.sun tut..)
> g.drawString("Hello world!", 50, 25);
g is whatever object it was assigned. This looks like something from a
Swing or AWT tutorial, which I don't use. More than likely, it refers to
a graphical object such as a jpane or applet.
All my stuff is command-line and JSP, so I'm not very familiar with the
Swing or AWT interfaces.
>
> what does "g." mean? (and what is diff. betw. drawString and
> System.out.println()?
drawString is a method of some graphical object, I assume. [IIRC from
reading an applet tutorial some time ago.]
System.out.println sends output to the operating system's default
output, usually a prompt.
> (also, what is 50, 25)??
>
I assume coordinates on an x/y axis of where to draw it.
You'd be best off reading more about object oriented programming before
going too much further. It is *very* different from linear javascript
and Perl. If you don't understand objects, you'll get very .
Java is ALL objects. Javascript has a few objects (window, document,
etc). I don't think Perl has any.
--
--
~kaeli~
Murphy's Law #3020: Quality assurance doesn't.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
| Frances Del Rio 2004-07-21, 8:58 pm |
| I love yr website.. my roommate recently adopted a cat, it turns out
she was pregnant, she had 4 kittens last Thurs. night, i.e., they're not
even a w old.. they're TOO CUTE!! Mom freaks out sometimes, grabs
kittens by the collar and takes them to cabinet under the kitchen
sink... other than that they all seem ok... :)
Frances
kaeli wrote:
> In article <2m7t12FjgssmU1@uni-berlin.de>, fdr58@yahoo.com enlightened
> us with...
>
>
>
> Java requires variables to be declared. Javascript does not.
> I could also have written
> int i = s.indexOf('k');
>
>
>
>
> That statement makes no sense. 'int' is a datatype. 'i' is a variable.
> Similarly:
> Integer i = new Integer(0);
> String s = new String("hello world");
> int i = 0;
> double i = 0.00;
>
> You would not write
> String = s;
>
> [int and double are simple data types. Integer and String are objects.]
>
> In javascript, you *can* declare variables, but you don't have to unless
> there's scope issues:
> var i = 0;
> var a = new Array();
>
> you would not write
> var = i;
>
> In VB:
> Dim i as Integer
> Dim s as String
>
>
>
>
> What's the difference? Not much. Probably memory allocation or
> something. I honestly don't know and have never had to use "new String"
> type syntax.
>
>
>
>
> g is whatever object it was assigned. This looks like something from a
> Swing or AWT tutorial, which I don't use. More than likely, it refers to
> a graphical object such as a jpane or applet.
> All my stuff is command-line and JSP, so I'm not very familiar with the
> Swing or AWT interfaces.
>
>
>
>
> drawString is a method of some graphical object, I assume. [IIRC from
> reading an applet tutorial some time ago.]
> System.out.println sends output to the operating system's default
> output, usually a prompt.
>
>
>
>
> I assume coordinates on an x/y axis of where to draw it.
>
> You'd be best off reading more about object oriented programming before
> going too much further. It is *very* different from linear javascript
> and Perl. If you don't understand objects, you'll get very .
> Java is ALL objects. Javascript has a few objects (window, document,
> etc). I don't think Perl has any.
>
| |
|
| In article <2m835cFjs4c9U1@uni-berlin.de>, fdr58@yahoo.com enlightened
us with...
> I love yr website.. my roommate recently adopted a cat, it turns out
> she was pregnant, she had 4 kittens last Thurs. night, i.e., they're not
> even a w old.. they're TOO CUTE!! Mom freaks out sometimes, grabs
> kittens by the collar and takes them to cabinet under the kitchen
> sink... other than that they all seem ok... :)
>
> Frances
>
>
Thanks!
Good luck with the kittens, and don't forget to get them "fixed". :)
--
--
~kaeli~
No one is listening until you make a mistake.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
| Frances Del Rio 2004-07-22, 3:59 pm |
| ok, I'm INCREDIBLY 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/tuto.../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...
(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???
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 , thank you again for your help.. Frances
kaeli wrote:
> In article <2m7t12FjgssmU1@uni-berlin.de>, fdr58@yahoo.com enlightened
> us with...
>
>
>
> Java requires variables to be declared. Javascript does not.
> I could also have written
> int i = s.indexOf('k');
>
>
>
>
> That statement makes no sense. 'int' is a datatype. 'i' is a variable.
> Similarly:
> Integer i = new Integer(0);
> String s = new String("hello world");
> int i = 0;
> double i = 0.00;
>
> You would not write
> String = s;
>
> [int and double are simple data types. Integer and String are objects.]
>
> In javascript, you *can* declare variables, but you don't have to unless
> there's scope issues:
> var i = 0;
> var a = new Array();
>
> you would not write
> var = i;
>
> In VB:
> Dim i as Integer
> Dim s as String
| |
|
|
| Roedy Green 2004-07-22, 3:59 pm |
| On Thu, 22 Jul 2004 12:09:47 -0400, Frances Del Rio <fdr58@yahoo.com>
wrote or quoted :
>(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???
Think of an int as like a pigeonhole in a computer. It is only big
enough to store a number about 9 digits long.
A long is a bigger pigeonhole (64 bits) which can hold a number about
19 digits long.
When you buy 256MB of ram you get 268,435,456 bytes of ram. Each int
takes 32 bits or 4 bytes. You have room to store 67,108,864 ints, or
33,554,432 longs.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
|
| In article <MPG.1b688a07108a46eb989fd6@nntp.lucent.com>,
tiny_one@NOSPAM.comcast.net enlightened us with...
>
>
> What's the difference? Not much. Probably memory allocation or
> something. I honestly don't know and have never had to use "new String"
> type syntax.
>
I found the difference.
Using new String() will always allocate new memory. If you just use '=',
if the string is already in the pool, it will point to it.
See this thread at Sun.
http://forum.java.sun.com/thread.js...40218&tstart=60
&trange=15
So it IS basically a memory allocation thing.
--
--
~kaeli~
Why did kamikaze pilots wear helmets?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
|
| In article <2ma76lFklqtkU1@uni-berlin.de>, fdr58@yahoo.com enlightened
us with...
>
> 1) you say below that int and integer are two differnt types (??)
Well, technically it's Integer and it's an object, not a type. The
capital 'I' is important.
> ("int and double are simple data types. Integer and String are
> objects.") ok, so 'string' is not a type of data but an object??
That's correct. A String is a sequential collection of characters (data
type char).
You listed the data types. String was not in there. 'char' was.
> so when we declare
>
> String str = "I like learning Java";
>
> this is not a variable but an object?? confusing...
Well, you're getting mixed up with the terminology. It IS a variable. It
is NOT a primitive data type. It IS an object.
Variables can be either objects OR primitive data types.
int i = 0;
String s = new String("Java rocks");
Both i and s are variables. Only s is an object. 'i' is a primitive data
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???
>
Largest the computer running the java program can store in that data
type. If you have a number, say 1024, what data type would you use? Most
of us would use int or Integer, but if you were really worried about
saving memory, you could fit it in a short.
Okay, let's look at this:
int i = 0;
String s = new String("hi");
(this is from what I understand from C/++ programming - someone should
correct me if I'm wrong on any of this for Java)
An int is a collection of bytes whose bits are all turned off or on to
represent a number. 0 would mean they are all off.
So what the first statement does is it gets a collection of bytes in
memory, sets all the bits in each to off, and makes the variable 'i'
point to that location. Now, 'i' will reference that location to get or
set values. That's all it does.
A String, however, is an object. The second statement first figures out
what size a String object is, allocates memory for it, runs through the
object's constructor with the given value, then returns a pointer to
that memory location to 's'. You can then call methods of the object
which may or may not change the value in the memory location.
An object has methods that can be called. A primitive does not.
--
--
~kaeli~
Why did kamikaze pilots wear helmets?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
| Roedy Green 2004-07-22, 3:59 pm |
| On Thu, 22 Jul 2004 13:01:18 -0500, kaeli
<tiny_one@NOSPAM.comcast.net> wrote or quoted :
>An int is a collection of bytes whose bits are all turned off or on to
>represent a number. 0 would mean they are all off.
>So what the first statement does is it gets a collection of bytes in
>memory, sets all the bits in each to off, and makes the variable 'i'
>point to that location. Now, 'i' will reference that location to get or
>set values. That's all it does.
not quite. local variable i is a slot on the stack. When you set i to
0 it puts a 0 in directly in that slot. It does not put the 0
somewhere on the heap then make i point to in.
If you said:
Integer ii = new Integer(0);
Then it creates an object on the heap with the value 0 in it, and
makes ii point to it.
Objects get this indirect approach. Primitive values go right in the
variable slot.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
|
| In article <fh20g0puctjm76chki37vm882ecq1lqkcq@4ax.com>, look-
on@mindprod.com.invalid enlightened us with...
> On Thu, 22 Jul 2004 13:01:18 -0500, kaeli
> <tiny_one@NOSPAM.comcast.net> wrote or quoted :
[color=darkred]
> not quite. local variable i is a slot on the stack. When you set i to
> 0 it puts a 0 in directly in that slot. It does not put the 0
> somewhere on the heap then make i point to in.
> If you said:
> Integer ii = new Integer(0);
> Then it creates an object on the heap with the value 0 in it, and
> makes ii point to it.
> Objects get this indirect approach. Primitive values go right in the
> variable slot.
Um, I thought that's what I said without actually using the stack and
heap, which the OP really isn't ready for understanding yet. If it
isn't, it's what I meant. ;)
--
--
~kaeli~
Doing my part to piss off the Religious Right.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
| |
| Grant Wagner 2004-07-29, 3:58 pm |
| Frances Del Rio wrote:
> ok, I'm INCREDIBLY 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/tuto.../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 , 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
| |
| Chris Smith 2004-07-29, 8:57 pm |
| kaeli wrote:
> Um, I thought that's what I said without actually using the stack and
> heap, which the OP really isn't ready for understanding yet. If it
> isn't, it's what I meant. ;)
The confusion comes from saying things like "Both i and s are variables.
Only s is an object." or especially "So what the first statement does is
it gets a collection of bytes in memory, sets all the bits in each to
off, and makes the variable 'i' point to that location."
One of the most important fundamental steps to take in learning Java is
to draw the distinction between variables and objects. There are no
exceptions to it. The first statement quoted above sounds as if an
object is a variable, which makes it confusing at the very least. The
latter makes it sound as if a variable is an object, which is equally
wrong (or at least misleading).
Roedy's terminology of stack and heap is equally valid as a description
of one model of possible implementation, and is useful for predicting
performance. It could be rejected from a rigorous language-definition
standpoint because it implies specific implementation. Nevertheless,
the distinction between objects (whose lifetime is independent of
lexical scope) and variables (which are closely tied to their scope and
die at the end of their containing block) is critically important.
Experience goes to show that failing to make that distinction clearly is
the source of much confusion about the Java programming language.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
| |
| Carl Smotricz 2004-07-29, 8:57 pm |
| 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:
>
>
>
>
> 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.
>
>
>
>
> 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.
>
>
>
>
> 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
>
| |
| Grant Wagner 2004-07-30, 8:57 pm |
| The reason you gave is why a byte can contain the values -128 to 127.
The reason I gave is why Byte.MAX_VALUE returns 127. The only reason Byte returns
127 is because someone at Sun 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;
If someone suddenly decided a primitive byte in the VM was 12 bits wide, Byte would
have be re-written to be:
public static final int MAX_VALUE = 2047;
(which would require casts anywhere Byte.MAX_VALUE is used)
The important point is there is no "magic" going on here, Byte.MAX_VALUE is just
returning a public static final byte that contains a particular value. By happy
coincidence (actually design) that particular value happens to match the maximum
value a primitive byte can hold, but Byte doesn't *know* that.
Carl Smotricz wrote:
> 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 <gwagner@agricoreunited.com>
|
|
|
|
|