AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Brian Ngure
Expertise
Strong in Java. Will be able to answer both in areas of designing and coding. Some knowledge in servlets

Experience
5 years of Java experience, Sun Certified Java Associate and Sun Certified Java Programmer, JDK 1.4

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Java Questions

Java - Java Questions


Expert: Brian Ngure - 9/2/2009

Question
QUESTION: Hey, I'm a Java newbie and I just ran into some confusing programs without explanation, can you help me with these? thanks

1) In the following program, why does the instance variable, engineState, has an initial value even though i didn't initialize it. Does this mean that, when not explicitly specified, the boolean variable has a automatic value of false?
class Motorcycle {
 String make;
 String color;
 boolean engineState;
 
 void startEngine() {
   if(engineState == true) System.out.println("The engine is already on!");
   else {
     engineState = true;
     System.out.println("The engine is now on."); }
 }
 
 void showAtts() {
   System.out.println("This motorcycle is a " + color + " " + make);//remember how + include variables
   if(engineState == true)
     System.out.println("The engine is on.");
   else System.out.println("The engine is off."); }
 //creating a class
 
 public static void main(String args[]) {
 Motorcycle m = new Motorcycle();
/*create a new object of class Motorcycle, very similar to dynamic allocation in C++*/
 m.make = "Yamaha RZ350";
 m.color = "yellow";//Here engineState is not initialized
 
 System.out.println("calling showAtts...");
 m.showAtts();//output here shows that engineState is off even though i didn't initialize
 System.out.println("......");
 System.out.println("Starting engine...");
 m.startEngine();
 System.out.println("......");
 System.out.println("Calling showAtts...");
 m.showAtts();
 System.out.println("......");
 System.out.println("Starting engines...");
 m.startEngine(); }//create an object and use the methods to make it do something
}

2) In this program, why can't the third date be displayed? Is the code out-of-date?
import java.util.Date;
import java.lang.String;

class CreateDates {
 public static void main(String args[]) {
   Date d1, d2, d3;
   
   d1 = new Date();//create an instance to hold today's date
   System.out.println("Date 1: " + d1);
   
   d2 = new Date(71, 7, 1, 7, 30, 25);//create an instance of year, month, day, hour(as in 24), minute, second
   System.out.println("Date 2: " + d2);
   
   d3 = new Date("April 3 1993 3:24PM"); // create an instance that is of the said date and time in a string(may be obsolete)
   System.out.println("Date 3: " + d3);     }
}

3) What is the definition of being the exact same object? why does having the same content and originate from the same class would result in different objects? And what is the different between String str = "me"; and str = new String("me");
class EqualsTest {
 public static void main(String args[]) {
   String str1, str2;
   str1 = "she sells sea shells by the sea shore";
   str2 = str1;
   
   System.out.println("String 1 " + str1);
   System.out.println("String 2 " + str2);
   System.out.println("Same object? " + (str1 == str2));
   
   str2 = new String(str1);
   
   System.out.println("String 1 " + str1);
   System.out.println("String 2 " + str2);
   System.out.println("Same object? " + (str1 == str2));
   System.out.println("Same content? " + str1.equals(str2));
 }
}

ANSWER: Hi,

1) Yes. Automatically false.
2) Put a space between the time and PM
3) Different objects because they both exist in different parts of memory.

  String str = "me"; and str = new String("me"); are the same thing (almost). First is a String literal and second is a String object.

str2 = str1; <--- Both variables points to same object

str2 = new String(str1);  <--- New object is created at new memory location


Brian

---------- FOLLOW-UP ----------

QUESTION: Hey, Brian,

    I'm not quite sure I understand you about Question 3, you said they are different objects because they exist in different parts of memory, if so, where? How can 2 objects be equal? Does that mean they come from the same class, have the same content, and lives in the same part of memory? But how can 2 objects live in the same location??

    Aside to these, can you tell me what is the difference between an array of arrays and a true multidimensional array? Please be specific.

Thank you

Answer
Hi,

It's not easy to find out where objects exist in memory in Java. In C or C++ you have pointers. Imagine a memory address for object 1 is 585649 and memory address for object 2 is 128946. Even though they come from the same class and have the same content, they are two different distinct objects.

When you do object1 = object2, you discard object 1 and both object 1 and object point to memory address 128946.

As for arrays, please look at these links for an in depth analysis:

http://www.cafeaulait.org/course/week2/51.html
http://www.cplusplus.com/doc/tutorial/arrays/

Brian

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.