1.Java的两种机制
Java虚拟机(Java virtual machine)
垃圾回收机制(Garbage Collector)
2.Java的 一些基础语法
标识符:以字母开头或一下划线_和$开头,后面可以是字母或数字不可以是其他特殊符号。
类名标识符,第一个字母大写其余小写,第二个单词首字母大写。
类的成员变量标识符,第一个单词小写,第二个单词首字母大写其余小写
main是启动方法
//标识符:以字母开头或以_和$开头,之后可以是字母或数字 //类名标识符,第一个字母大写,其它字母小写,第二单词字母大写 public class StudentClass { int stuAge; //stuName 类的成员变量标识符,第一个字母小写,其它字母小写,第二单词字母大写,其它字母小写 String stuName; //main方法名标识符,第一个字母小写,其它字母小写,第二单词字母大写,其它字母小写 //main方法是启动方法 //args 变量标识符,第一个字母小写,其它字母小写,第二单词字母大写,其它字母小写 public static void main(String[] argString) { // TODO Auto-generated method stub //argString 它是一个局部变量 System.out.println("111"); } //showMethod方法名标识符,第一个字母小写,其它字母小写,第二单词字母大写,其它字母小写 public void showMethod() { stuName="wangweifeng"; } }
Java 数据类型
基本数据类型:1.数字类型:整数 byte(8) short(16) int(32) long(64)
浮点数 float(32) double(64)
2.布尔类型:boolean
3.字符型:char
public class TestInt {
public static void main(String[] args) { //数字类型 //整数 byte 8, short 16 int 32 long 64 byte varByte=-128; int varInt=129; long varLong=3434345333L; long varLong1=3434345333L; //强制类型转换,谁转换谁负责, varByte=(byte)varInt; System.out.println(varByte); //浮点数 float 32 double 64 float varFloat=3.123456789012345678f; System.out.println(varFloat); double varDouble=3.123456789012345678D; System.out.println(varDouble); varDouble=3; System.out.println(varDouble); //布尔类型 boolean boolean b=true; b=false; if(3>5) { System.out.println("true"); }else { System.out.println("false"); } //字符类型 char char varChar=20013; int a='中'; System.out.println(varChar); System.out.println(a); String str="中国"; //它不是基本数据类型,它是引用类型
long inta=3+3l; }
}
java中可以从任意基本类型转型到另外的基本类型
例外 boolean 类型不可以转换为其他的数据类型。
转换分为默认转换和强制转换 整形,字符型,浮点型的数据在混合运算中相互转换,转换时遵循以下原则:
容量小的类型默认转换为容量大的数据类型;数据类型按容量大小排序为: byte,short,char->int->long->float->double byte,short,char之间不会互相转换,他们三者在计算时首先回转换为int类型 容量大的数据类型转换为容量小的数据类型时,要加上强制转换符,但可能造成精度降低或溢出;使用时要格外注意。 有多种类型的数据混合运算时,系统首先自动的将所有数据转换成容量最大的那一种数据类型,然后再进行计算。
运算符:
算术运算符: +,-,*,/,%,++,
-- 关系运算符: >,<,>=,<=,= =,!=
逻辑运算符: !,& , | , ^ , &&,||
&&短路与,第一条件为false,后边不运行 &与,第一条件为false,后边还会运行
位运算符: &,|,^,~ , >>,<<,>>>
赋值运算符: =
扩展赋值运算符:+ =,- =,* =,/ =
字符串连接运算符:+
循环语句
条件语句 - 根据不同条件,执行不同语句。 if
if .. else
if .. else if
if .. else if .. else if .. else
switch
循环语句 – 重复执行某些动作 for (JDK1.5语法)
while
do .. While
switch
while 先判断在执行
do......while
1.要有初始化的值
2.要有一个条件判断,能够进入循环
3.修改条件的语句能退出循环
代码暂时不写。
数组
声明数组的语法格式有两种如下:
数组元素类型 数组名[ ];
数组元素类型[ ] 数组名;
例如:char s[]; Point p[]; 或:
char[] s; Point[] p;
数组
1.同种类型放在一起
2.数组具有不变性,一旦确定数组个数不可扩展
3.会进行初始化,数字类型为 0;char为' ' ;boolean为false;引用类型全部为null.
public class ArrayTest {
public static void main(String args [] ) {
int [] intArr; intArr=new int[5]; intArr[1]=35; System.out.println(intArr[1]); for (int i : intArr) { System.out.println(i); } char charArr[]; charArr=new char[5]; for (char c : charArr) { System.out.println(c); } String [] strArry=new String[5]; for (String string : strArry) { System.out.println(string); } }
}