2- 创建java工程
| 类型 | 描述 | bit | 最小值 | 最大值 |
| byte |
8位整数
|
8 | -128 (-2^7) | 127 (2^7-1) |
| short |
16位整数
|
16 | -32,768 (-2^15) | 32,767 (2^15 -1) |
| int |
32位整数
|
32 |
- 2,147,483,648 (-2^31) |
2,147,483,647 (2^31 -1) |
| long |
64位整数
|
64 |
-9,223,372,036,854,775,808 (-2^63) |
9,223,372,036,854,775,807 (2^63 -1) |
| float |
32位实数
|
32 | -3.4028235 x 10^38 | 3.4028235 x 10^38 |
| double |
64位实数
|
64 | -1.7976931348623157 x 10^308 | 1.7976931348623157 x 10^308 |
| boolean |
逻辑类型
|
|
false | true |
| char |
字符
|
16 | '\u0000' (0) | '\uffff' (65,535). |
package com.h3.tutorial.javabasic.variable;
public class variableexample1 {
public static void main(string[] args) {
// declare a variable of type int (integer 32-bit)
int firstnumber;
// assigning values to firstnumber
firstnumber = 10;
system.out.println("first number =" + firstnumber);
// declare a 32-bit real number (float)
// this number is assigned a value of 10.2
float secondnumber = 10.2f;
system.out.println("second number =" + secondnumber);
// declare a 64-bit real numbers
// this number is assigned a value of 10.2
// character d at the end to tell with java this is the type double.
// distinguished from a float.
double thirdnumber = 10.2d;
system.out.println("third number =" + thirdnumber);
// declare a character
char ch = 'a';
system.out.println("char ch= " + ch);
}
}
package com.h3.tutorial.javabasic.variable;
public class variableexample2 {
public static void main(string[] args) {
// declare three 64-bit integer (long)
long firstnumber, secondnumber, thirdnumber;
// assign value to firstnumber
// l at the end to tell java a long type, distinguished from type int.
firstnumber = 100l;
// assign values to secondnumber
secondnumber = 200l;
// assign values to thirdnumber
thirdnumber = firstnumber + secondnumber;
system.out.println("first number = " + firstnumber);
system.out.println("second number = " + secondnumber);
system.out.println("third number = " + thirdnumber);
}
}
if(condition1 true) {
// do something here
}elseif(condition2 true) {
// do something here
}elseif(condition3 true) {
// do something here
}else { // other
// do something here
}
package com.h3.tutorial.javabasic.controlflow;
public class elseifexample1 {
public static void main(string[] args) {
// declaring a integer number (int)
int score = 20;
system.out.println("your score =" + score);
// if the score is less than 50
if (score < 50) {
system.out.println("you are not pass");
}
// else if the score more than or equal to 50 and less than 80.
else if (score >= 50 && score < 80) {
system.out.println("you are pass");
}
// remaining cases (that is greater than or equal to 80)
else {
system.out.println("you are pass, good student!");
}
}
}
int score = 80;
package com.h3.tutorial.javabasic.controlflow;
public class elseifexample2 {
public static void main(string[] args) {
// declare a variable int simulate your age.
int age = 20;
// test age less than or equal 17
if (age <= 17) {
system.out.println("you are 17 or younger");
}
// test age equals 18
else if (age == 18) {
system.out.println("you are 18 year old");
}
// test age, greater than 18 and less than 40
else if (age > 18 && age < 40) {
system.out.println("you are between 19 and 39");
}
// remaining cases (greater than or equal to 40)
else {
// nested if statements
// test age not equals 50.
if (age != 50) {
system.out.println("you are not 50 year old");
}
// negative statements
if (!(age == 50)) {
system.out.println("you are not 50 year old");
}
// if age is 60 or 70
if (age == 60 || age == 70) {
system.out.println("you are 60 or 70 year old");
}
}
}
}
package com.h3.tutorial.javabasic.controlflow;
public class booleanexample {
public static void main(string[] args) {
// declare a variable of type boolean
boolean value = true;
// if value is true
if (value == true) {
system.out.println("it's true");
}
// else
else {
system.out.println("it's false");
}
// with boolean values you can also write
if (value) {
system.out.println("it's true");
}
// else
else {
system.out.println("it's false");
}
}
}
switch( variable_to_test ) {
casevalue:
// code_here;
break;
casevalue:
// code_here;
break;
default:
// values_not_caught_above;
}
package com.h3.tutorial.javabasic.controlflow;
public class switchexample1 {
public static void main(string[] args) {
// declare a variable age
int age = 20;
// check the value of age
switch (age) {
// case age = 18
case 18:
system.out.println("you are 18 year old");
break;
// case age = 20
case 20:
system.out.println("you are 20 year old");
break;
// remaining cases
default:
system.out.println("you are not 18 or 20 year old");
}
}
}
you are 20 year old
// this is not allowed !! case(age < 18) : // case only accept a specific value eg: case18: // do something here break;
package com.h3.tutorial.javabasic.controlflow;
public class switchexample2 {
public static void main(string[] args) {
// declare a variable age
int age = 30;
// check the value of age
switch (age) {
// case age = 18
case 18:
system.out.println("you are 18 year old");
// case age in 20, 30, 40
case 20:
case 30:
case 40:
system.out.println("you are " + age);
break;
// remaining case:
default:
system.out.println("other age");
}
}
}
you are 30
for( start_value; end_value; increment_number ) {
// code here
}
packagecom.h3.tutorial.javabasic.loop;
publicclass forloopexample1 {
publicstaticvoidmain(string[] args) {
// declare a variable, step in loop
intstep = 1;
// declare a variable value with the start value is 0
// after each iteration, value will increase 3
// and the loop will end when the value greater than or equal to 10
for(intvalue = 0; value < 10; value = value + 3) {
system.out.println("step ="+ step + " value = "+ value);
// increase 1
step = step + 1;
}
}
}
step =1 value = 0 step =2 value = 3 step =3 value = 6 step =4 value = 9
package com.h3.tutorial.javabasic.loop;
public class forloopexample2 {
public static void main(string[] args) {
int sum = 0; for (int i = 0; i <= 100; i = i + 1) {
sum = sum + i;
}
system.out.println(sum); }
}
5050
// while the condition is true, then do something.
while( 条件为真 ) {
// do something here.
}
参见图示
publicclasswhileexampe1 {
publicstaticvoidmain(string[] args) {
intvalue = 3;
// while the value is less than 10, the loop is working.
while( value < 10) {
system.out.println("value = "+ value);
// increase value by adding 2
value = value + 2;
}
}
}
// the do-while loop to work at least one round
// and while the condition is true, it also works to
do{
// do something here.
}while( condition );
package com.h3.tutorial.javabasic.loop;
public class dowhileexample1 {
public static void main(string[] args) {
int value = 3;
// do-while loop will execute at least once
do {
system.out.println("value = " + value);
// increase 3
value = value + 3;
} while (value < 10);
}
}
value = 3 value = 6 value = 9
// declare an array, not a specified number of elements.
int[] array1;
// initialize the array with 100 elements
// the element has not been assigned a specific value
array1 = newint[100];
// declare an array specifies the number of elements
// the element has not been assigned a specific value
double[] array2 = newdouble[10];
// declare an array whose elements are assigned specific values.
// this array with 4 elements
long[] array3= {10l, 23l, 30l, 11l};
package com.h3.tutorial.javabasic.array;
public class arrayexample1 {
public static void main(string[] args) {
// declare an array with 5 elements
int[] myarray = new int[5];
// note: the first element of the array index is 0:
// assigning values to the first element (index 0)
myarray[0] = 10;
// assigning values to the second element (index 1)
myarray[1] = 14;
myarray[2] = 36;
myarray[3] = 27;
// value for the 5th element (the last element in the array)
myarray[4] = 18;
// print out element count.
system.out.println("array length=" + myarray.length);
// print to console element at index 3 (4th element in the array)
system.out.println("myarray[3]=" + myarray[3]);
// use a for loop to print out the elements in the array.
for (int index = 0; index < myarray.length; index++) {
system.out.println("element " + index + " = " + myarray[index]);
}
}
}
array length=5 myarray[3]=27 element 0 = 10 element 1 = 14 element 2 = 36 element 3 = 27 element 4 = 18
package com.h3.tutorial.javabasic.array;
public class arrayexample2 {
public static void main(string[] args) {
// declare an array with 5 elements
int[] myarray = new int[5];
// print out element count
system.out.println("array length=" + myarray.length);
// using loop assign values to elements of the array.
for (int index = 0; index < myarray.length; index++) {
myarray[index] = 100 * index * index + 3;
}
// print out the element at index 3
system.out.println("myarray[3] = "+ myarray[3]);
}
}
array length=5 myarray[3] = 903
package com.h3.tutorial.javabasic.javastructure;
public class person {
// this is field
// the name of person
public string name;
// this is a constructor
// use it to initialize the object (create new object)
// this constructor has one parameter
// constructor always have the same name as the class.
public person(string persionname) {
// assign the value of the parameter into the 'name' field
this.name = persionname;
}
// this method returns a string ..
public string getname() {
return this.name;
}
}
package com.h3.tutorial.javabasic.javastructure;
public class persontest {
public static void main(string[] args) {
// create an object of class person
// initialize this object via constructor of class person
// specifically, edison
person edison = new person("edison");
// class person has the method getname()
// use the object to call getname():
string name = edison.getname();
system.out.println("person 1: " + name);
// create an object of class person
// initialize this object via constructor of class person
// specifically, bill gates
person billgate = new person("bill gates");
// class person has field name (public)
// use objects to refer to it.
string name2 = billgate.name;
system.out.println("person 2: " + name2);
}
}
person 1: edison person 2: bill gates
package com.h3.tutorial.javabasic.javastructure;
public class fieldsample {
// this is static field.
public static int my_static_field = 100;
// this is normal field.
public string myvalue;
// constructor
public fieldsample(string myvalue) {
this.myvalue= myvalue;
}
}
package com.h3.tutorial.javabasic.javastructure;
public class fieldsampletest {
public static void main(string[] args) {
// create the first object.
fieldsample obj1 = new fieldsample("value1");
system.out.println("obj1.myvalue= " + obj1.myvalue);
// print out static value, access via instance of class (an object).
system.out.println("obj1.my_static_field= " + obj1.my_static_field);
// print out static value, access via class.
system.out.println("fieldsample.my_static_field= "
+ fieldsample.my_static_field);
// create second object:
fieldsample obj2 = new fieldsample("value2");
system.out.println("obj2.myvalue= " + obj2.myvalue);
// print out static value, access via instance of class (an object)
system.out.println("obj2.my_static_field= " + obj2.my_static_field);
system.out.println(" ------------- ");
// set new value for static field.
// (or using: fieldsample.my_static_field = 200)
obj1.my_static_field = 200;
// it will print out the value 200.
system.out.println("obj2.my_static_field= " + obj2.my_static_field);
}
}
obj1.myvalue= value1 obj1.my_static_field= 100 fieldsample.my_static_field= 100 obj2.myvalue= value2 obj2.my_static_field= 100 ------------- obj2.my_static_field= 200
package com.h3.tutorial.javabasic.javastructure;
public class finalfieldexample {
// a final field.
// final field does not allow to assign new values.
public final int myvalue = 100;
// a static final field.
// final field does not allow to assign new values.
public static final long my_long_value = 1234l;
}
package com.h3.tutorial.javabasic.javastructure;
public class methodsample {
public string text = "some text";
// default constructor
public methodsample() {
}
// this method return a string
// and has no parameter.
public string gettext() {
return this.text;
}
// this is a method with one parameter string.
// this method returns void (not return anything)
public void settext(string text) {
// this.text reference to the text field.
// distinguish the text parameter.
this.text = text;
}
// static method
public static int sum(int a, int b, int c) {
int d = a + b + c;
return d;
}
}
package com.h3.tutorial.javabasic.javastructure;
public class methodsampletest {
public static void main(string[] args) {
// create instance of methodsample
methodsample obj = new methodsample();
// call gettext() method
string text = obj.gettext();
system.out.println("text = " + text);
// call method settext(string)
obj.settext("new text");
system.out.println("text = " + obj.gettext());
// static method can be called through the class.
// this way is recommended. (**)
int sum = methodsample.sum(10, 20, 30);
system.out.println("sum 10,20,30= " + sum);
// or call through objects
// this way is not recommended. (**)
int sum2 = obj.sum(20, 30, 40);
system.out.println("sum 20,30,40= " + sum2);
}
}
text = some text text = new text sum 10,20,30= 60 sum 20,30,40= 90
package com.h3.tutorial.javabasic.inheritance;
public class animal {
public animal() {
}
public void move() {
system.out.println("move ...!");
}
public void say() {
system.out.println("<nothing>");
}
}
package com.h3.tutorial.javabasic.inheritance;
public class cat extends animal {
// override method of the animal class.
public void say() {
system.out.println("i am cat");
}
}
package com.h3.tutorial.javabasic.inheritance;
public class dog extends animal {
// override method of the animal class.
public void say() {
system.out.println("i am dog");
}
}
package com.h3.tutorial.javabasic.inheritance;
public class ant extends animal {
}
package com.h3.tutorial.javabasic.inheritance;
public class animaltest {
public static void main(string[] args) {
// declaring a cat object.
cat cat = new cat();
// check 'cat' instance of animal.
// the result is clearly true.
boolean isanimal = cat instanceof animal;
system.out.println("cat instanceof animal?"+ isanimal);
// ==> meo
// call the method say() of the cat.
cat.say();
// declare an object animal
// initialize the object through the constructor of the cat.
animal cat2 = new cat();
// ==> meo
// call to say() of cat (not animal)
cat2.say();
// create the object animal
// through the constructor of the class ant.
animal ant = new ant();
// ant has no say() method.
// it call to say() method that inherited from the parent class (animal)
ant.say();
}
}
cat instanceof animal?true i am cat i am cat <nothing>















