Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Need Help For Exam .

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,

I have one week for the exam,and i am not able to get good score so what should i do.I have read K&b 2 times but iam not able to get the flow of the programs.My percentage of mock exams are around is 50 - 55%.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Syed Naimathullah:
...iam not able to get the flow of the programs...


Can you post some specific code that you are having difficulty with, and tell us how you are approaching it? I'm sure we can help clear up the misconceptions.
 
Syed Naimathullah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
program from mock exam java belt
explain program in full detail

01: class MyClass {
02: static int maxElements;
03:
04: MyClass(int maxElements) {
05: this.maxElements = maxElements;
06: }
07: }
08:
09: public class Q19 {
10: public static void main(String[] args) {
11: MyClass a = new MyClass(100);
12: MyClass b = new MyClass(100);
13:
14: if (a.equals(b))
15: System.out.println("Objects have the same values");
16: else
17: System.out.println("Objects have different values");
18: }
19: }

i thought the output will be
Prints "Objects have the same values"
but the output is "Objects have different values"
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you think they had the same values?

The answer to this question will help you understand what's going on here. If you examine your thinking and assumptions, you'll be one step closer to which assumptions led you astray.

Here's a hint: what's going on with lines 11 and 12?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another hint: How exactly does the equals method work?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone explain the code???
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone explain the code???
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more hint: Just think what happens on line 14 which contains the check "a.equals(b)?".

Where is the equals method present to get executed? If at all does NOT exist anywhere, you should have got a compiler error during compilation itself.

As you had run the program, there was NO compiler error. So it should exist somewhere.

Where it exists?
What it does?


Just look for the answers to these questions. You will get some more level of closeness to the ACTUAL Answer.
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ssumthing wrong with MyClass a = new MyClass(100);

using Myclass???
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong about MyClass a = new MyClass(100); nor about the code.

Just think about those points everyone posted here...
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think bcoz None of the objects have any value. Static members belong to the class.so equal wont b overridden.

Please correct if i m wrong
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain i m rite or not???/
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the equals() method when inherited from Object only returns true if the two object references point to the same object'eg
 
Khaya Ndlovu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the equals() method when inherited from Object only returns true if the two object references point to the same object'eg
MyClass obj1 = new MyClass(100);
MyClass obj2 = new MyClass(100);
obj1 =obj2; //they now refer to the same object
 
Khaya Ndlovu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what we test when we say obj1==obj2???the thing is still nt very clear....so what is the difference between obj1==obj2 and obj1 equals(obj2)
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:
so what is the difference between obj1==obj2 and obj1 equals(obj2)

The difference is that == is always the same whereas you can overwrite equals to implement your own comparison.
 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Dont mean to be rude ... but you got to go through K&B again ... and solve all the 2 minute drills ... because this a very fundamental and straight forward question .... if you read the book throughlly you should'nt have these douts !!!
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here, Object class's equal method is being overridden . It will return true only if two references are same.

Read Object class equal method explanation.

"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)"

Try overriding equals() method of Object class in MyClass. You will see the difference.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see this related post (by the same original poster): doubt in "equals() and ==".
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Line 2 of the code , the variable "maxElements" is declared as static .
and again this variable is initialized through a constructor .
Can a static variable be initialized through a constructor ?
I think we need to use the "static " initialization block to initialize the static instance variables .
Please correct me If this is wrong .
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prathibha ananthapadmanabha:
Can a static variable be initialized through a constructor ?

Try it.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Pratibha!!!well you see the code beloww which i have highlighted....




in the code this.maxElements refers to static variable declared in the class whereas in the constructor (int maxElements),it is the argument which is passed when the method is called,so two different maxElements integers ........i hope it helps....
 
prathibha ananthapadmanabha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
01: class MyClass {
02: static int maxElements;
03:
04: MyClass(int elm) {
05: maxElements = elm;
06: }
07: }
08:
09: public class Q19 {
10: public static void main(String[] args) {
11: MyClass a = new MyClass(100);
12: MyClass b = new MyClass(100);
13:
14: if (a.equals(b))
15: System.out.println("Objects have the same values");
16: else
17: System.out.println("Objects have different values");
18: }
19: }

hi dhwani ,
sorry Iam not so clear ,
pls see the code above where I have changed the parameter for constructor .
Even then the instance variable is static only na .
so can static variables be initialized through a constructor .

Please advice.

Regards,
Prathibha.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prathibha ananthapadmanabha:
so can static variables be initialized through a constructor.

Did the program compile and run?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by prathibha ananthapadmanabha:
Even then the instance variable is static only na .



Prathibha, please try to use Real Words in JavaRanch Forums as its one of the policies being followed here.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey pratibha

i tried the above given program i am getting the output
as well,
may be i am not sure with the below statement......
i can say static variables when initialised in a constructor act as instance variables only.......
i hope it helps.......
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry for this kind of reply but please i really need to know this:

i want to know if all these topics are there
in SCJP 1.5 or not as they are not present in kathy sierra 5 but while
doing mock i could encounter few questions:
methods appendtail and appendreplacement from matcher class.
classes FilenameFilter,MathResult and Formatter.
method math.entrySet.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shikha,

Welcome to JavaRanch. First off, the methods you asked about are NOT on the exam, phew

Second, it's much better to start your own thread when you have a new question like this - thanks!

Bert
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic