![]() |
|
[JAVA] Pratyush13's "Guide to learning Java" - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: [JAVA] Pratyush13's "Guide to learning Java" (/Thread-JAVA-Pratyush13-s-Guide-to-learning-Java) |
[JAVA] Pratyush13's "Guide to learning Java" - Solixious - 09-15-2011 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.. Code: class FirstProgram
{
public static void main(String args[])
{
System.out.print("This is my first java program.");
}
}Code: class FirstProgramCode: 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. RE: [JAVA] Pratyush13's "Guide to learning Java" - Solixious - 09-15-2011 I'm posting some more codes here so that you get some more necessary concepts required for basic programming in java... Code: class DataTypes
{
public static void main(String args[])
{
int a=10; //int types store integer values
float b=5.32; //float types store decimal values
char c='k'; //char type store a single character value
System.out.print("Integer : "+a+" Float : "+b+" Character : "+c); //output all those values in single line (somewhat like cascading in c++)
}
} |