public class Puppy{ // Puppy 类 一个源文件中只能有一个public类 int puppyAge; public Puppy(String name){ // 这个构造器仅有一个参数:name System.out.println("Passed Name is :" + name ); }
public void setAge( int age ){ puppyAge = age; }
public int getAge( ){ System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; }
final double PI = 3.1415927; //常量指不能改变的量,用final标志,通常用大写标识
1 2 3
int decimal = 100; // 十进值 int octal = 0144; // 8进值 int hexa = 0x64; // 16进值
字符串
双引号
1 2 3
"Hello World" "two\nlines" "\"This is in quotes\""
1 2
char a = '\u0001'; // 变量a 的值为 '\u0001' String a = "\u0001";
4. 变量类型
变量类型
在Java语言中
局部变量
实例变量
类变量
所有的变量在使用前必须声明。声明变量的基本格式如下:
type identifier [ = value][, identifier [= value] ...] ;
1 2 3 4 5
int a, b, c; // 声明三个int型整数:a、b、c。 int d = 3, e, f = 5; // 声明三个整数并赋予初值。 byte z = 22; // 声明并初始化z。 double pi = 3.14159; // 声明了pi。 char x = 'x'; // 变量x的值是字符'x'。
局部变量
1 2 3 4 5 6 7 8 9 10 11 12
public class Test{ public void pupAge(){ int age = 0; // age是一个局部变量。定义在pupAge()方法中,它的作用域就限制在这个方法中 age = age + 7; System.out.println("Puppy age is : " + age); }
public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
public static void main(String args[]) { int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("a-- = " + (a--) ); // 查看 d++ 与 ++d 的不同 System.out.println("d++ = " + (d++) ); // 当前语句后 +1 System.out.println("++d = " + (++d) ); // 当前语句前 +1 } }
关系运算符
RelationalOperator.java
1 2 3 4 5 6 7 8 9 10 11 12 13
public class RelationalOperator {
public static void main(String args[]) { int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } }
逻辑运算符
LogicalOperators.java
1 2 3 4 5 6 7 8 9
public class LogicalOperators { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b)); } }
赋值运算符
AssignmentOperator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class AssignmentOperator { public static void main(String args[]) { int a = 10; int b = 20; int c; c = a + b; System.out.println("c = a + b is " + (a + b)); System.out.println("c += a is " + (c+=a) ); System.out.println("c -= a is " + (c -=a)); System.out.println("c *= a is " + (c *= a)); a = 10; c = 15; System.out.println("c /= a is " + (c/=a) ); a = 10; c = 15; System.out.println("c %= a is " + (c %= a)); } }
条件运算符
ConditionalOperator.java
1 2 3 4 5 6 7 8 9 10
public class ConditionalOperator { public static void main(String args[]){ int a , b; a = 10; b = (a == 1) ? 20: 30; // 不成立 System.out.println( "Value of b is : " + b ); // 30 b = (a == 10) ? 20: 30; // 成立 System.out.println( "Value of b is : " + b ); // 20 } }
instanceOf 运算符
Car.java
1 2 3 4 5 6 7 8 9
class Vehicle {}
public class Car extends Vehicle { // Car 继承了Vehicle所有属性和方法 public static void main(String args[]){ Vehicle a = new Car(); boolean result = a instanceof Car; // 被比较的对象兼容于右侧类型,该运算符仍然返回true System.out.println( result); // true } }
7. 分支结构
if else
IfElse.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class Test {
public static void main(String args[]){ int x = 30;
if( x == 10 ){ System.out.print("Value of X is 10"); } else if( x == 20 ){ System.out.print("Value of X is 20"); } else if( x == 30 ){ System.out.print("Value of X is 30"); } else{ System.out.print("这是 else 语句"); } } }
嵌套的if…else语句
1 2 3 4 5 6 7 8 9 10 11 12 13
public class Test {
public static void main(String args[]){ int x = 30; int y = 10;
if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } }