The Artima Developer Community
Sponsored Link

Java Answers Forum
primitiveType[1] Confusing Java POS

2 replies on 1 page. Most recent reply: Jun 9, 2005 11:04 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

primitiveType[1] Confusing Java POS Posted: Jun 9, 2005 6:30 AM
Reply to this message Reply
Advertisement
Hi Folks:

Going through the Java Pos documentation, I'm trying
to understand why someone would create and return
or utilize a primitive value carried as a single
array element. I thought Java has no dereferencing
as in other languages (hence memory address is would
not be a reason).

For instance why would someone declare variables as
follows:

boolean var1[1];
int var2[1];

Why not just say:

boolean var1;
int var2?

Thanks in advance.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: primitiveType[1] Confusing Java POS Posted: Jun 9, 2005 6:38 AM
Reply to this message Reply
Its Ok, I got the answer:

Wrapper types for primitives e.g Integer and Boolean
do not support modification and ints and boolean
are passed by value.

int var[0] = x;
var[0] = y;

has a different effect

Integer var = new Integer(x);
var = new Integer(y);

Only that I don't see why this would make a difference
in Java Pos.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: primitiveType[1] Confusing Java POS Posted: Jun 9, 2005 11:04 PM
Reply to this message Reply
The difference lies in:

var2 = var1

If var1 is int[0], by altering var1[0] also var2[0] is altered.

if var1 is Integer, you can only alter it this way:
var1 = new Integer(newVal).
In this case the value of var2 would not be altered.

Flat View: This topic has 2 replies on 1 page
Topic: length variable in arrays Previous Topic   Next Topic Topic: installing netbeans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use