JAVA 入门
JAVA语言的特点:1.简单易用 2.安全可靠 3.跨平台性 4.面向对象 5.支持多线程
数据类型转换:
1.自动类型转换:①两种数据类型彼此兼容②目标类型的取值范围大于源类型的取值范围
2.强制类型转换:不满足上边条件
比如: byte 可以通过自动类型转换为int 而 int 转byte则会报错
变量的作用域:一个大括号中
常量:在程序中固定不变的值,包括:整型常量,浮点数常量,布尔常量,字符常量等.
特殊常量: true,false ,null
逻辑运算符:&& / || 短路与 短路或
-
运算符的优先级:
优先级越上越大
if 语法:
if()
{}
else if()
{}
else
{}
switch 语法:
switch(表达式):{
case 目标值1:
执行语句1
break;
case 目标值2:
执行语句2
break;
...
default :
执行语句n
break;
}
方法格式: 类似于函数
修饰符 返回值类型 方法名 ([参数类型 参数名1,参数类型 参数名2,..]){
执行语句
return 返回值;
}
public static int getArea(int x ,int y){
int temp=x*y;
return temp;
}
方法的重载:
会根据参数个数或是类型的不同自动匹配合适的方法
重载的条件:1.方法名相同 2. 参数个数或参数类型不相同
数组
定义方法: int []x=new int[10]
int[]x; x=new int [10]
int []x={3,2,5,8,4,7,6,9}
数组名.length
数组常见操作:
1.数组遍历:
int x[]= {3,2,5,8,4,7,6,9};
for (int i=0;i<x.length;i++)
{
System.out.println(x[i]);
}
2.数组最值
int x[]= {3,2,5,8,4,7,6,9};
System.out.println("数组的最大值为:"+getMax(x));
}
public static int getMax(int a[]) {
int max=a[0];
for (int i=1;i<a.length;i++) {
if (max<a[i]) {
int t=a[i];
a[i]=max;
max=t;
}
}
return max;
}
3.数组排序
public static void sort(int a[]) {
for (int i=0;i<a.length;i++)
{
for (int j=i;j>0;j--)
{
if (a[j]<a[j-1]) {
int t;
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
for (int i=0;i<a.length;i++)
{
System.out.print(a[i] +" ");
}
}
Arrays 工具类:
Arrays 是一个专门用于操作数组的工具类,该类位于java.util包中.Arrays 工具类提供了大量的静态方法,常用的方法如表所示:
int x[]= {3,2,5,8,4,7,6,9};
int copy[]=Arrays.copyOfRange(x, 1, 7);//前闭后开
System.out.println(Arrays.toString(copy));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
Arrays.fill(x,9);
System.out.println(Arrays.toString(x));
String 类和StringBuffer类
String 类
初始化两种方法:
1.String str1="abc";
2.String str1=new String();
String str2=new String("abc")
String类的常用方法
indexOf :返回指定字符(子字符串)在此字符串中第一次出现处的索引
charAt 查看指定下标对应的字符
endsWith 判断此字符串是否以指定的字符串结尾
length 返回此字符串的长度
equals 将此字符串与指定的字符串比较,如果相等则返回true,否则返回false
isEmpty 当且仅当字符串长度为0时返回true
startsWith 判断此字符串是否以指定的字符串开头
contains 判断此字符串是否包含指定的字符序列
toLowerCase 将字符串全部转换为小写
toUpperCase 将字符串全部转换为大写
toCharArray 将此字符串转换为一个字符数组
replace 返回一个新的字符串,它是通过利用newstr替换此字符串中出现的所有的oldstr得到的
split 根据参数字符串将原来的字符串分割为若干个子字符串
substring 返回一个新字符串 ,它包含从指定的beginIndex处开始,直到此字符串末尾的所有字符
返回一个新字符串 ,它包含从指定的beginIndex处开始,直到此字符串endIndex-1的所有字符
trim 返回一个新的字符串,它去除了原字符串首尾的空格
String str1=" ababcabcde dcba ";
System.out.println(str1.length());
System.out.println(str1.charAt(1));
System.out.println(Arrays.toString(str1.toCharArray()));
System.out.println(str1.toUpperCase());
System.out.println(str1.toLowerCase());
System.out.println(str1.endsWith("ddd"));
System.out.println(str1.trim());
System.out.println(Arrays.toString(str1.split("b")));
System.out.println(str1.substring(5));
System.out.println(str1.contains("cabc"));
System.out.println(str1.replace("ab", "oo"));
21
[ , , a, b, a, b, c, a, b, c, d, e, , d, c, b, a, , , , ]
ABABCABCDE DCBA
ababcabcde dcba
false
ababcabcde dcba
[ a, a, ca, cde dc, a ]
bcabcde dcba
true
oooocoocde dcba
StringBuffer类:
由于String 字符串是常量,一经创建,其内容和长度不可改变.如果需要对一个字符串进行修改,则只能创建新的字符串.为此JDK中提供了一个StringBuffer类(也称作字符串缓冲区),与String类不同的是,它的内容和长度是可变的.StringBuffer类如同一个字符串容器,当在其中添加或删除字符时,并不会产生新的StringBuffer对象.
针对添加和删除字符的操作,StringBuffer类提供了一系列的方法,具体如下
StringBuffer append(char c):添加参数到StringBuffer对象中
StringBuffer insert(int offset,String str):在字符串中的offset位置插入字符串str
StringBuffer delete(int start,int end) :删除StringBuffer对象中指定范围的字符或字符串序列
StringBuffer deleteCharAt(int index) :移除此序列指定位置的字符
StringBuffer replace(int start,int end,String s):
void setCharAt(int index,char ch) :修改指定位置Index处的字符序列
StringBuffer reverse():将此字符序列用其反转形式取代
String toString() :返回StringBuffer缓冲区中的字符串
add();
remove();
alter();
public static void add() {
StringBuffer sb=new StringBuffer();
sb.append("abcd");
System.out.println(sb);
sb.insert(1, "shshshs");
System.out.println(sb);
}
public static void remove()
{
StringBuffer sb=new StringBuffer("abcdefg");
sb.delete(2, 4);
System.out.println(sb);
sb.deleteCharAt(2);
System.out.println(sb);
sb.delete(0, sb.length());
System.out.println(sb);
}
public static void alter()
{
StringBuffer sb=new StringBuffer("abcdefg");
sb.setCharAt(1, 'd');
System.out.println(sb);
sb.replace(0, 3, "123");
System.out.println(sb);
System.out.println(sb.reverse());
}
abcd
ashshshsbcd
abefg
abfg
adcdefg
123defg
gfed321
包装类
在JAVA中,很多类的方法都需要接收引用类型的对象,每个基本数据类型都有其对应的包装类。
char --Character long --Long
byte -- Byte float --Float
int --Integer double--Double
short--Short boolean--Boolean
除了Integer 和 Charcter 之外,其他包装类的名称就是第一个字母大写
包装类和基本数据类型在进行转换时,引入了装箱和拆箱的概念,其中装箱是指将基本数据类型的值转为引用数据类型;反之,拆箱是指将引用数据类型对象转换为基本数据类型。
拆箱和装箱的过程:
Integer a=3;//自动装箱
int b=a+4;//自动拆箱
syso{b};
java 获得键盘输入值:
public static void main(String [] args) throws IOException{
System.out.print(“Enter a Char:”);
char i = (char) System.in.read();
System.out.println(“your char is :”+i);
}
虽然此方式实现了从键盘获取输入的字符,但是System.out.read()只能针对一个字符的获取,同时,获取进来的变量的类型只能是char,当我们输入一个数字,希望得到的也是一个整型变量的时候,我们还得修改其中的变量类型,这样就显得比较麻烦。
2.我们需要用到BufferedReader类和InputStreamReader类
public static void main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
System.out.println(“Enter your value:”);
str = br.readLine();
System.out.println(“your value is :”+str);
}
方法三:这种方法我认为是最简单,最强大的,就是用Scanner类
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入你的姓名:”);
String name = sc.nextLine();
System.out.println(“请输入你的年龄:”);
int age = sc.nextInt();
System.out.println(“请输入你的工资:”);
float salary = sc.nextFloat();
System.out.println(“你的信息如下:”);
System.out.println(“姓名:”+name+“\n”+“年龄:”+age+“\n”+“工资:”+salary);
}