[JAVA] Pratyush13's "Guide to learning Java" 09-15-2011, 10:41 AM
#1
Pratyush13's Guide to learning Java
Have latest version (changes with time) of j.d.k. (java developmental kit) installed in your computer.
I prefer to jump straight onto programming part and then explain each part in it. So here goes the first program..
Let me explain what is happening in it..
In the first line, we specify a class named "FirstProgram". You can replace this with a name of your choice. Just be careful that the name of your choice should not start with a numerical value and should not contain any special character except underscore. Whatever is going to be the content of the program we are making will be encapsulated by this class we specify.
"public" is the access specifies. This can be replaced by "private", or "protected" or it can be simply left blank which would give it a default characteristics of being accessed by any other classes. Being "public" specifies that it can be called/accessed by a class anywhere.
"static" gives such a characteristic to this method/function that it can be called by other classes without creating an object of this class (an object of a class is used to access the non-static member functions and member variables of the class).
"void" is a return type which means that this object will return no value wherever it is being called. Some examples of other return types are "int" which returns an integer value, "float" which returns a decimal value, etc.
"main" is the name of the function we are creating. It is the "main" function that is executed when we execute a java program irrespective of what other functions exist in the class.
"String args[]" is the parameter of the main function. It means that this function will accept an array named "args" of "String" type. The values passed as parameters can be used the the method to complete some task.
It sends the output as "This is my first java program." on our screen when it is executed.
Curly braces are used to specify various blocks of the program we are writing.
That's it! You've made your first java program.
Save the file with name same as that of class name you provided and an extension of ".java"(without quotes).
To create the byte code of java by compiling it, go to the location of your file using command prompt and type "javac [filename]"
If their were no errors, you can view the output of this program by typing "java [classname]"
If their was some error, correct it and compile again.
About bytecode : It is a special code generated when a java program is compiled. This code can be executed in any OS irrespective of the one it was created in (j.d.k. should be present in that machine in which it is transfered). This is what makes java a platform independent language.
Example of calling a method..
Compile both the above classes and execute class B. This was an example of calling methods/functions of one class in another class.
NOTE : Whatever is written after a double forward slash(//) are comments and is ignored by the compiler till the end of that line.
This was my 1st tutorial post. Thanks for reading. I hope I didn't mess it up.
Have latest version (changes with time) of j.d.k. (java developmental kit) installed in your computer.
I prefer to jump straight onto programming part and then explain each part in it. So here goes the first program..
Code:
class FirstProgram
{
public static void main(String args[])
{
System.out.print("This is my first java program.");
}
}
Code:
class FirstProgram
Code:
public static void main(String args[])
"static" gives such a characteristic to this method/function that it can be called by other classes without creating an object of this class (an object of a class is used to access the non-static member functions and member variables of the class).
"void" is a return type which means that this object will return no value wherever it is being called. Some examples of other return types are "int" which returns an integer value, "float" which returns a decimal value, etc.
"main" is the name of the function we are creating. It is the "main" function that is executed when we execute a java program irrespective of what other functions exist in the class.
"String args[]" is the parameter of the main function. It means that this function will accept an array named "args" of "String" type. The values passed as parameters can be used the the method to complete some task.
Code:
System.out.print("This is my first java program.");
Curly braces are used to specify various blocks of the program we are writing.
That's it! You've made your first java program.
Save the file with name same as that of class name you provided and an extension of ".java"(without quotes).
To create the byte code of java by compiling it, go to the location of your file using command prompt and type "javac [filename]"
If their were no errors, you can view the output of this program by typing "java [classname]"
If their was some error, correct it and compile again.
About bytecode : It is a special code generated when a java program is compiled. This code can be executed in any OS irrespective of the one it was created in (j.d.k. should be present in that machine in which it is transfered). This is what makes java a platform independent language.
Example of calling a method..
Code:
class A
{
public static void method1() //creating a static method
{
System.out.print("I'm from class A, method1");
}
public void method2() //creating a non static method
{
System.out.print("I'm from class A, method2");
}
}
class B
{
public static void main(String args[])
{
System.out.print("Calling methods of class A. ");
A.method1(); //calling a static method
A a=new A(); //creating an object 'a' of class 'A' in order to access its non static members
a.method2(); //calling a non static method of class A
}
}
NOTE : Whatever is written after a double forward slash(//) are comments and is ignored by the compiler till the end of that line.
This was my 1st tutorial post. Thanks for reading. I hope I didn't mess it up.
Folow me on My YouTube Channel if you're into art.