aspose file tools*
The moose likes Beginning Java and the fly likes Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variables" Watch "Variables" New topic
Author

Variables

loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
Hi. Sorry for bothering you but I'm having some problems with reference variables. Can someone
tell me the answers for the codes below. If I translated something wrong from my language to English please tell me and I will try to fix it.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

What are your best tries at the answers? Give those and we'll help you with them, but you won't get someone else to just give you the answers here.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
Bear Bibeault wrote:What are your best tries at the answers? Give those and we'll help you with them, but you won't get someone else to just give you the answers here.


ah, sorry about that. I think the answers are
1. b)
2. a)
3. I made a mistake, forgot to write the code
int a;
String b;

And the answer is a)

4. b)
5. b)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

OK, but we don't know why you picked those answers. For question, explain why you chose the answer. Otherwise, we have no way of knowing if you're getting right by accident, or why an answer might be wrong.
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
well that's why i'm asking for help. I gave my best shot but I'm asking for a second opinion or they might be the wrong answers.
For example in the second question String is an object but don't have an value like for example String b = "Hello" so my doubts are
if there was created an object or not.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

My point is, if you say the answer is (b), we need to know why you picked that, regardless of whether it's right or wrong, to be sure that you're understanding the concept.
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
I picked that because there was declared an variable and has bean initialized with a value so that variable points to something, is a reference...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

OK, cool. What about the rest of your answers?
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
2. There are none objects created because String isn't initialized with a value, so that object isn't created yet.
3. I have doubts here and I chose a) just because int is an primitive variable just like byte,short,long,double,float,boolean,char. But String isn't initializes so I don't know if
that variable is a reference or not
4. There was created an object because with the keyword new we crate objects and the synax is Object nameOfThatObject = new Object;
5.I chose a because as I said int is part of primitive(simple) variables of datatypes
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

3. Does the fact that the variable is not initialized change the type of the variable?

4. I think this one is ambiguous. Do they intend to count the string literal as an object or not?

5. By "simple" did they mean "primitive"? If so, ok.
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
3. I've learned that byte,short,int,long,float,double,boolean,char are primitive (simple) variables(datatypes) and String is an object.
So it don't seam right to chose c) and call String primitive.

4. Yes my instructors accept String as an object. But problem is that String strA don't have an value. So is that object created or not?

5. Yes simple and primitive are same
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

3. Yes, c is not correct. b is a ref variable even if it currently does not have a reference.

4. new creates a new object. But the string literal also represents an object, likely from the string pool. So it depends if the people making the questions count that one or not. As I said, I think it's a poorly worded question.
loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
I figured out and these are the answers
1. b) - String is an reference
2. a) - int is an primitive variable and String is an reference
3. a) - same as 2nd answer
4. b) - first String is lateral so it doesn't count as object, and second String strB = new String("hello"); - this is an object
5. b) - int is an simple datadype, String is not.

Thanks for your time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variables