Saturday, September 27, 2008

Not to depend on the Dumps or Question Banks!!

Hello friends,

First my apologies for not being regular in posting exciting articles as before. I will be regular now onwards.

I am back with a piece of advice for you all. Please do not depend on the dumps/question bank or any other source which anyone provides you saying,"hey, just read these 200 questions and you will get most of the question from this only!" That sentence is partially true! But partially! You will never score high if you depend only on these question banks. Yes they claim that these are real questions from exams remembered from the people who have v the exams, and many other ways - I don't know. But it's the truth that these will only degrade you. Studying java passionately and then going and giving the exam needs guts, practice and will power to do it - lets what ever the result may be. And if you really study hard SCJP is really not that difficult! Keep this in mind!!

The number of questions repeated are very very low as compared to what the people selling these question sets say. And anyways knowledge lives forever, what if in the future you cleared SCJP with 90% and were not able to make a proper synchronized method, or unable to solve a simple NullPointerException! That will be so insulting for you! Clear the SCJP with knowledge and hard work, and it will definately pay you back!

Thursday, September 11, 2008

What are inner classes?


This topic is not for beginners or those at intermediate level. Not very difficult but may result in headache sometimes. So lets understand what are inner classes or nested classes - as some call them.


Inner class - as the name says are inside a class. Normal classes which we see are called outer classes because they are directly inside a file and not inside another class. Hey, if you are wondering whether a class can be inside a class?? Yes, it can be. But there are various complications in its use, and I suggest in real life not to use it unless they are unavoidable. But SCJP exam contains questions about such complex things..so its a very important concept. And hey its not boring, its very interesting topic... I will try to make it more interesting!

OK, let's see how a inner class looks like...

class A{
int a;

class B{
int b;

}//class B ends

}// class A ends

You can clearly see that B is inside A. When we create object of class A it doesn't mean we create object of inner class B also. We have to explicitly create object of inner class B. The inner classes can also be static! Yes, they can be. Also there can be more than one inner classes inside a class. You can create objects of them independently. Inner classes can have variables and methods as other classes... the members can be static, final etc.. The concept of Inner classes is not that simple, I thought to keep the first post easy, I will come with the difficult scenarios related to inner classes soon...

If you have any basic questions about inner classes, they do fire them in the comments section.. also try to code yourself, that is first thing to do when you don't understand anything.

Sunday, August 31, 2008

Enum - tricky ones...

I was reading something regarding enums yesterday and found two interesting things which I feel I should share with my readers. These are very simple things for those who already know it but may be surprising for those who don't...

- Enums are not expandable at runtime.
So, suppose you have somethings like
enum Car { HONDA, MERC };
then you cannot add another car say FORD as 3rd constant in the list at runtime. You can have as many references you can to point to the enum constants.

- ; is unnecessary at the end of enum definition! :)
Yeh, this is quite funny and surprising, don't know why java developers didn't do it mandatory. Exam may have questions with multiple code sentences in single line, so look out properly where ; is necessary and where it isn't. And now u know that there is no need of ; after } when the enum definition completes.

Thanks!

Sunday, August 24, 2008

Types of Variables...

Well it is important to know what are the types of variables because there behaviour and their life and death is directly related to their type! There are 6 types of variables and I classify them on basis of 2 things to make you understand better.

- Depending upon what a variable holds they are classified as
1. Primitive Variable
2. Reference Variable

- Depending upon where they are created they are classified as
1. Class variable ( also called static variables)
2. Instance variable
3. Method Parameter
4. Local Variable.

Let's go one by one to understand them. A primitive variable is one which holds the primitive e.g: an int, a char etc. A reference variable is one which holds an object of any class, it can be java API class like String or your wn created class. A class variable is one which is a static variable and inside the class definition. This variable is created when the class loads in the memory and stays there till the class is in the memory. A class variable is not attached to any instance of that class. A instance variable is a non-static member of which is attached to the specific instance of the class. It is reated when a class instance is created and stays in memory untill that instance is in the memory. A method parameter is one which we write in method definition to accept the method arguments the method call is passing to it. This thing will be clear after you see the following example. And lastly a local variable is the one which is declared inside a method body, it is also sometimes called method variable.

Lets take an example to show each type of variable and identify there types..
class City
{
static int name;
Lake l;
int road;
void getDistance(City c )
{
int distance;
}
}
Here,
distance is method variable,
c is method parameter,
road is instance variable,
l is reference variable,
name is class variable
I hope you got it now.. if not fire the doubts in comments! :)

Saturday, August 9, 2008

Some strange facts about Constructors...


Although you would have written many constructors before, I am sure you will find some suprises in following facts about constructors. To start with a constructor is nothing but a piece of code of class with same name as that of class which is called whenever an object is created of that class. Now understand following things one by one...

1.If you don't write a constructor for yourself the compiler will insert one - this is call a default constructor. Constructors can have arguments and there can be a no-argument constructor. Compiler inserted constructor will always be a no-arg constructor.

2. Constructors can take any number of arguments, even var-args i.e.: variable arguments. (More on this in a few days.)

3. Constructors cannot be marked final or static or abstract. Because final, static and abstract keywords are for methods, variables and classes. You can consider that constructors implicitly have final kind of behaviour because they are never inherited! They are not methods beware!

4. Constructors cannot have a return type! This is a very important and perhaps the only criteria to recognise quickly whether the given entity is a method or constructor. A method should have return type but a constructor should never have a return type. A method can have same name as that of the class - its perfectly legal! :)

5. Constructors are never inherited! Again...they aren't methods!! You can remember like this - "Constructors are not passed to the younger generation."

6. Whenever a object has to be created then the constructor of that class has to get called and also the constructors of all its super classes should get called. For example if A is parent of B and B is parent of C then to create an object of C we will call C's constructor which in turn will automatically call the constructors of class B and which inturn will call A's constructor and which inturn will call Object's constructor - as by default class A is child of class Object! Remember that the automatically inserted super class constructor call will be always to the no-arg constructor. If you find it a bit difficult then fire your questions in comments.

7. Constructors can use any access modifier. They can use private, protected and public access modifiers. And if nothing is written they have the default one- package level access. Start thinking why anyone would want to make a private constructor! :)

8. Constructors can be over loaded and hence there can be more than one constructors for a single class. A constructor can call other constructor by the keyword this. A call to another constructor with single argument looks like this(i); .A constructor can even explicitly call its super class constructor by keyword super. A call to parent's constructor with no argument looks like super(); . The first statement of every constructor should be a call to this or super.

9. And here's the most stuning one - Abstract classes have constructors! They are called whenever any object of its subclass is created. So the bottom line is every class has a constructor.

10. Interfaces do not have a constructor. An interface is just a contract, a contract to fulfill the implementation of some methods, remember that.

11. You can have call to both this( ) and super( ) in the same constructor. You can have them one inside other, but not both in any single constructor.

Hoossh.. that's all what I can remember right now.. curse my neurotic RAM! If I find anything else I will update the list. If you have any question, then do comment..as you have seen I reply to every question asked.
C ya,

Saturday, July 19, 2008

Can a local variable be static..? uhh..why not?

No! - that's a very straight forward answer to that question and most simple one. In java the local variables can not be static - burn this in your mind. But Why??. The local variables are those which are declared inside a method body, so they have scope only for the method execution period. Any static thing in java belongs to the class and not to the instance. Whenever the method is called the method variables are in the memory area of the thread executing that method, so they are on stack. On the other hand the objects and static members of a class are on heap. Static things are to live throughout the method calls and when they are inside the method body (on stack) they can't live outside the scope of method, hence its ridiculous and illogical to have static variables inside a method body.
For the exam, you need not know why, just remember that a local variable can never be static - compiler will complain.

Tuesday, July 8, 2008

File name and class name should be same?


There is a relation between the name of java source file and the public classes you can declare in that file - only the public classes..okay..not classes with any other access level. Hey by the way do you know which access modifiers can be applied to the outer classes/non-inner classes?...that will be a fantastic post in itself...coming soon.. :) So, coming back to the point, yes there is a constraint on the file name when you want to declare a class in the file as public. The constraint is that the file name should be same as that of your public class name.

But it's not that simple to understand by this single sentence, when I heard first time about this thing I had following doubts:

1. Can I have more than one public classes in my java file?

2. Should the class be public to be called by the JVM ?

3.If a class is not public but has a main method can it be called by the JVM? If yes then how the hell is it possible??? And if not ...why?? what's the problem in that??

There is no simple solution to these questions than coding for yourself and thats what I did!

Take a look at the code carefully, it's in the java source file named as example6.java

Let me tell you some things in this example. The source file name is example6.java and there is no class called as example6 in the source file. There are two classes class1 and class2 which are not public!

Doubts Answered:

1. NO! I tried to give public access to class1 and class2 and compiler scolded me for two things - one he said that class1 if public should be declared in file named class1.java, other is - there can't be any two public classes in a single java file.

2. Not al all! You can see that class1 and class2 are not public, still I am calling both of them!

3. Yes it can be called if it has a main method! You can see that class1 is not public but has a main method, so I can call it by giving parameter class1 to the java command.

I hope you have throughly understood the constraints. Just to revise all, here is the conclusion...

Conclusion: The above program proves that...

1.There is no need that the classes whose main method you are going to call should be public.

2.File name can be anything as long as you dont have any public classes in your file.

3.Which main method will execute only depends upon which classname you pass to the JVM that is as argument to java command.

4. If you want global access of a class and hence declare that class as public then it should be in the same file as the class name, and as you cant have two different names for a single file you can't have two public classes in a single .java file!

Sunday, July 6, 2008

Difference: positive zero 0 and negative zero -0 ?



Recently I have come across some strange things. I know that java language takes positive and negative zero differently than our logical mathematical brain will take. Well the two are certainly different but how does java identifies them? Or more basic question... does java even differenciate between positive and negative zero? The truth is that IEEE floating point specification differentiates between positive and negative zero, but the java specification says that positive zero is equal to negative zero. Please go through the following code and see if you can satisfy your questions...

I am still not able to understand why I am not getting -0 at 2nd and 4th place. I have posted this in javaranch forums, lets see if I can get some clarification. If you have the answer please comment.

Wednesday, July 2, 2008

Java is pass by value.....Always! - Part2

In the last post we saw what exactly pass by value and pass by reference means and strictly speaking its a fact that there is nothing like pass by reference in java. So we can say that java is pass by value...always! This post is a continuation so if you havn't please go through the first part - Java is pass by Value.....Always! - Part1

However note that, for the clarity of understanding the pass by value is when we pass a value and pass by reference is when we pass a reference to something.
Now lets look at the code which demonstrates pass by value and pass by reference.

Suppose there is a class TV, which contains a string which shows its state and its setters and getters.(Note:For the time please forget about the access modifiers..etc because this is just to make you understand one concept..I havnt run this program so it may have some errors/exceptions. But this code is perfect to make you understand what you need to at this point of time.)


class TV
{
string state; // this stores whether TV is currently ON or OFF

TV(string h)
{
this.state=h; //constructor assign a state for new TV object
}

setState(string s)
{
this.state=s;
}

string getState()
{
return(state);
}
}

The following code will demonstrate the pass by value with variable i and pass by reference with references remote1 and remote2.

class example1
{
int i=2;
public static void main()
{
TV remote1= new TV("OFF");
func2(i);
func3(remote1);
}

func2(int j)
{
System.out.println(j); // this will print 2
j=3;
System.out.println(j); // this will print 3
System.out.println(i); // this will print 2
}


func3(TV remote2)
{
System.out.println(remote2.getState()); //this will print OFF
remote2.setState("ON");
System.out.println(remote1.getState()); //this will print ON
System.out.println(remote2.getState()); //this will print ON
}

}


Here i and j are variables and they are independant of eachother. remote1 and remote2are references and they point to the same object, so both can turn on and off the same TV!

Have some doubts still? Go..on.. post a comment.. I will try my best to answer.

Sunday, June 29, 2008

Rules for legal identifiers...


The SCJP exam wants you to know perfectly the rules which define legal and illegal identifiers names. Make sure you know this perfectly as there is no excuse for this. Read the following carefully and you will understand it's not that difficult:

Rules for Legal Identifier Names:

1. Identifiers must start with a letter, a currency character ($), or a connectingcharacter such as the underscore ( _ ). Identifiers cannot start with a number!

2. After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.

3. In practice, there is no limit to the number of characters an identifier cancontain.

4. You can't use a Java keyword as an identifier. Following figure lists all of the Java keywords including one new one for 5.0, enum.

5. Identifiers in Java are case-sensitive; so pen and PEN are two different identifiers.

The list of Java 1.5 Keywords follow -

Some of the legal identifiers are:

$aturn, _$23, _23, $_aturn, there_is_no_limit_to_the_number_of_characters

Some illegal identifiers are:

23$, -saturn, :saturn, saturn#, .saturn

If you have any questions...let them come through the comments, I will gladly answer each one of them.

Friday, June 27, 2008

Java is pass by value.....Always! - Part1


Surprised? Well, lets look at what is pass by value and pass by reference. Let us understand first what is a value and what is a reference. A value of any variable/or java object is the value assigned to it....huh...now what does this means? More technically, a variable is nothing but a name given to a memory location and the value of x is what is stored at the memory location of x. A reference is a pointer to an object. Say its like a remote control through which you can handle the target object. Take help of following figures...

Variable X storing value 7

Reference X storing a remote control

Clearly understand the difference between a value and reference and then you can understand what is pass by value and pass by reference. Well basically as far as java is concerned there is no difference!!Thats why I said java is pass by value always. Let's see why there is no difference. In a pass by value you always pass the underlying value of that variable...that means what ever is stored at the memory place. So if X is a variable and it has value 7 then the memory location contains the number 7. And when you pass by value you actually pass what is stored in the memory..and so 7 is passed! When X is a reference then also same thing happens...whatever is stored at the memory location is passed. But hey wait..in earlier case memory contained 7, but what is here? Here X is a reference, hence it contains a remote control to someone whom it is pointing to..so the memory contains the remote control! So here the remote control is passed! In both cases the value is passed, in first case the value is the actual value and in next case the value is actually the remote control. When you pass the remote control the entity (to which you are passing the remote control) can manipulate the object. In the next post I will demonstrate code for both the cases and then you will be able to point out the exact difference between pass by value and pass by reference. Hey, by pass by reference we mean passing the value..which is infact the remote control(the reference!) :)

Saturday, June 21, 2008

Other than SCJP...what lies ahead?


This post will tell you about other Sun Java Certifications. Although many know only SCJP there are other plethora of Sun Java Certifications. Lets look at them, hey this knowledge isn't necessary for an SCJP taker to know but we should know where we are heading, right?

Sun Java Certification is itself a ladder and as you climb up the technology becomes more advanced and powerful. Many consider that SCJP is the 1st and most basic exam in the ladder but there is one more, its called SCJA - Sun Certified Java Associate Exam. These two are entry level exams in the ladder and you need to pass SCJP before you can give any of further examinations. There are core differences between the syllabus of SCJA and SCJP, like the "swing" is not included in SCJP but it is in SCJA. There is a difference between a java programmer and a java developer. You can call yourself a java developer after you give the special java developer exam called SCJD - the Sun Certified Java Developer exam. You are supposed to have the basic knowledge of java as a language after you complete SCJP and SCJD but you still dont know how to use the java language to the fullest i.e: developing web/standalone commercial products/applications from it - thats the path ahead. There are 4 certifications up the ladder each gives you a special knowledge about the corresponding technology.
They are...
1. SCWCD - the Sun Certified Web Component Developer exam.
2. SCBCD - the Sun Certified Business Component Developer exam.
3. SCDJWS - the Sun Certified Developer for Java Web Services exam.
4. SCMAD - the Sun Certified Mobile Application Developer exam.

The SCWCD portion is about the web components you need to build a web application. You should know how to develope a web application from scratch, before giving SCWCD. The SCBCD concentrates upon the business components required in an application, it includes in-depth study of the EJB (Enterprise Java Beans) technology. I donn't know about the SCDJWS. The next one SCMAD is concerned about knowledge about developing the java mobile applications. Hey the mobile in your pocket if it's java enabled (if it's cost is above 8k, mostly it is.) can run java applications developed by you! You may be thinking these certifications aren't that famous (so may be unimportant) but let me tell you that in the software industry, these special level certifications are valued the most. As SCJP is a pre-requisite for all these you need to pass SCJP before giving any of these. The ladder doesn't end here, there is one more certification at the top, and this is the highest one, it called SCEA - the Sun Certified Enterprise Architect exam. I really don't know what the portion is at this level and what knowledge we need to have... :)


I hope this post has helped you to know where we are and how far the journey is!

Saturday, June 14, 2008

My level of java knowledge right now...i.e.: before starting preparation...

Now after knowing Java from past 3 years my teachers will be ashamed if I say "I am pretty new to java." So I am not new to java, I know java from past 3 years and yes have done pretty good amount of coding in it. But knowing java and passing the Sun certification examinations are two different things. You don't need to have working knowledge of java to do programs and even projects in it, but the Sun certification examinations demand that you should not only know good ways of programming in java but also know bad ways of programming in java. A normal java programmer will write a code which runs fine, but a SCJP programmer is supposed to write the most efficient code - and that's where you should know the intracacies and details of the programming language. So, here I am, I am able to write java programs..sometimes very long ones, but nah nah.. I don't even know were they efficient or not.

Talking to the point, I have gone through most basic Java programming books like the Complete Reference by Herbert Schildt, some chapters of Core Java and even some chapters of SCJP preparation books by Kathy Seirra & Bert Bates. I havn't given even a single Mock Exam and havn't started preparation for the examinations as such. So, this is where I am beginning. Let's start now.... join me.

What this blog is about?

Hello friends,

Today I have decided to give the SCJP exam. You will find anything and everything related to SCJP over here. You will find the SCJP information, posts stating my journey of study..yeh you got it right...technical posts (and no philosophy..eek..!:) ) and in the process indirectly I will be sharing the knowledge with you all. This is a perfect place for people who are planning to give SCJP in near future or are preparing for it. The most interesting thing about this blog is that it will contain "my journey of facing SCJP" and that means I havn't yet given the exam and I will start preparing for SCJP. It's named "facing SCJP" because who knows I will pass or fail! :)

Warm Regards,
Saurabh