Saturday, July 19, 2008
Can a local variable be static..? uhh..why not?
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.
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 ?
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
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...
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.
5. Identifiers in Java are case-sensitive; so pen and PEN are two different identifiers.
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...
Reference X storing a remote control
Saturday, June 21, 2008
Other than SCJP...what lies ahead?
They are...
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...
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?
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