//Console commands
$ javac hello.java //compiles hello.java file and produces hello.class
$ java hello //runs hello class
$ java hello xyz //runs hello class with parameter, send parameters to java main method which must be created with the syntax
public class Hello {
public static void main (String[] args){
//is start method of a java class and not requires return value so it
//is void, static so it is kept on the ram memory, and also static
//methods not requires to create an object to call.
System.out.println("Hello"+args[0]); //arg[0] gets first parameter which is xyz at command line
}
}
Creating an object
public class Xyz{ //class name must start with the capital letter
public void methodX(){ //method name should start with small letter, this method is not static so for to call this method requres to create Xyz object.
System.out.println("written X"); // for eclipse easy way to write System.out.println is just type "syso" and press shift+space
}
public static void main (String args[]){ // init method of Xyz class
Xyz xyz = new Xyz(); // which means create an object with the name of control xyz from Xyz
xyz.methodX(); // call method X over the xyz control
}
}
//javadoc
//creates referance document from comments in the class which written in /** blablabla */
//for to create javadoc just type $ javadoc classname
@see //referances javadoc to another java class or method. @see classname#methodname
@version //versionNr defines version of the document
@return //defines return description of method for javadoc
objects and assignment
class Number {
int i;
}
public class Assignment{
public static void main (String[] args){
Number nr1 = new Number(); //creates nr1 object
Number nr2 = new Number(); //creates nr2 object
nr1.i = 1; //assign 1 to nr1 object i value
nr2.i = 0; //assign 0 to nr2 object i value
nr1 = nr2; //assign
//referenced object of nr2 to nr1. important point is that just reference
//point is changed, nr1 value which is 1 is still on the memory but nr1
//is now pointing the object which nr2 points. nr1 = 0 and nr2 = 0
nr1 = 2;
// now nr1 and nr2 is pointing same object and when we assing a value
//to nr1 it also change the value of nr2 because they using and calling
//same object. nr1 = 2 and also important point is that also nr2 = 2
}
// now what about the object which used to nr1 and contains 1 value? it is not used so garbage collector will destroy it.
}
Objects comparations
public class comparation{
public static void main (String[] args){
Integer o1 = new Integer(100)
Integer o2 = new Integer(100)
o1 == o2; //false - because it says object o1 is referencing same object with object o2 NO
o1 != 02; //true - opposite of first case
}
}
//Controls
while (x<y){ ... }
do { ... }while(x
for (int i = 0, int j = 1; i<20 && j > 10; i++, j--) { ... } // we can use 2 variable in a for loop
if (method1() || method2()) { ... } else if (method1() < method2()){ ... }
switch (x) { case 1: .... break; case 2: ... break}
labelxxx:
for(int i = 0; i<10; i++){
for(int j = 0; j<10; j++){
...
break labelxxx;
}
}
continue;
return;
//Constructor
class ExampleClassForConstructor {
public ExampleClassForConstructor(){ //same name with class name, starts with capital character
...
}
}
public class ExampleClassConstructorCaller {
public static void main (String[] args){
new ExampleClassForConstructor();
}
}
System.gc();
Fi
$ javac hello.java //compiles hello.java file and produces hello.class
$ java hello //runs hello class
$ java hello xyz //runs hello class with parameter, send parameters to java main method which must be created with the syntax
public class Hello {
public static void main (String[] args){
//is start method of a java class and not requires return value so it
//is void, static so it is kept on the ram memory, and also static
//methods not requires to create an object to call.
System.out.println("Hello"+args[0]); //arg[0] gets first parameter which is xyz at command line
}
}
Creating an object
public class Xyz{ //class name must start with the capital letter
public void methodX(){ //method name should start with small letter, this method is not static so for to call this method requres to create Xyz object.
System.out.println("written X"); // for eclipse easy way to write System.out.println is just type "syso" and press shift+space
}
public static void main (String args[]){ // init method of Xyz class
Xyz xyz = new Xyz(); // which means create an object with the name of control xyz from Xyz
xyz.methodX(); // call method X over the xyz control
}
}
//javadoc
//creates referance document from comments in the class which written in /** blablabla */
//for to create javadoc just type $ javadoc classname
@see //referances javadoc to another java class or method. @see classname#methodname
@version //versionNr defines version of the document
@return //defines return description of method for javadoc
objects and assignment
class Number {
int i;
}
public class Assignment{
public static void main (String[] args){
Number nr1 = new Number(); //creates nr1 object
Number nr2 = new Number(); //creates nr2 object
nr1.i = 1; //assign 1 to nr1 object i value
nr2.i = 0; //assign 0 to nr2 object i value
nr1 = nr2; //assign
//referenced object of nr2 to nr1. important point is that just reference
//point is changed, nr1 value which is 1 is still on the memory but nr1
//is now pointing the object which nr2 points. nr1 = 0 and nr2 = 0
nr1 = 2;
// now nr1 and nr2 is pointing same object and when we assing a value
//to nr1 it also change the value of nr2 because they using and calling
//same object. nr1 = 2 and also important point is that also nr2 = 2
}
// now what about the object which used to nr1 and contains 1 value? it is not used so garbage collector will destroy it.
}
Objects comparations
public class comparation{
public static void main (String[] args){
Integer o1 = new Integer(100)
Integer o2 = new Integer(100)
o1 == o2; //false - because it says object o1 is referencing same object with object o2 NO
o1 != 02; //true - opposite of first case
}
}
//Controls
while (x<y){ ... }
do { ... }while(x
for (int i = 0, int j = 1; i<20 && j > 10; i++, j--) { ... } // we can use 2 variable in a for loop
if (method1() || method2()) { ... } else if (method1() < method2()){ ... }
switch (x) { case 1: .... break; case 2: ... break}
labelxxx:
for(int i = 0; i<10; i++){
for(int j = 0; j<10; j++){
...
break labelxxx;
}
}
continue;
return;
//Constructor
class ExampleClassForConstructor {
public ExampleClassForConstructor(){ //same name with class name, starts with capital character
...
}
}
public class ExampleClassConstructorCaller {
public static void main (String[] args){
new ExampleClassForConstructor();
}
}
System.gc();
Fi
No comments:
Post a Comment