LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2005, 04:31 PM   #1
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Rep: Reputation: 15
basic java programming question


help!

please concider the followwing code:
Code:
Customer c;  //c will refer to a customer
c = new Customer();  //c refers to a newly created customer
simmilarly:
Code:
	int[] myarray;
	myarray = new int[10];
the first excerpt is from my csc textbook. the second performs a simmmilar operation.

i sort of get that these both "create" a new object(?, pointer?, instance?) of whatever you are trying to initialize(?). but (as you might have guessed) im still a little fuzzy on understanding it all completely.

could anyone tell me

A)what exaclty is this doing

and B) what is the difference between what the first line does and the second line does.


thanks for the help.
 
Old 09-21-2005, 04:45 PM   #2
Jestrik
Member
 
Registered: Dec 2003
Location: Hampshire, UK
Distribution: gentoo, Debian Sarge, Slack 9.1
Posts: 206

Rep: Reputation: 30
The first line of the code declares the object in memory. The second line instantiates the object.

Think of it like a person -

The first line declares the name of the person.
The second line gives meaning to the person, i.e. brings it to life and gives it properties and sets what type of person this is.

Hmm, I'm not very good at explaining this, try and look for some tutorials on Google, i'm sure they're better than me!

Last edited by Jestrik; 09-21-2005 at 04:51 PM.
 
Old 09-21-2005, 11:46 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,378

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
Jestrik is right. In other (loose) terms, think of line 1 as defining/declaring a data structure ( eg C struct ) and line 2 as producing an 'instance' of that structure (with data) that you can actually use.
Chris
PS Hi to Jestrik from another Hampshire-ite
 
Old 09-22-2005, 01:37 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
In Java, object values are effectively what are pointers in other languages. When you declare "Customer c;", it is initially set to NULL. Then when you do "new Customer()", it allocates memory and creates the object; then "c = ..." makes "c" point to this new object.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM
java programming question! redhatrosh Programming 2 03-15-2005 12:51 PM
Basic Linux Programming Question (QT terminal control) Boffy Programming 5 09-09-2004 12:44 PM
A very basic newbie question on Java lmellen Programming 5 07-19-2004 03:59 PM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 01:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration