变量在使用前必须赋值
/**
CircleArea: Compute the area of a circle with given radius
*/
public class CircleArea{
public static void main( String[] args ){
// define the PI constant
final double PI = 3.14159;
// declare the radius variable
double radius;
// declare the area variable
double area;
// assign value to variable radius
radius = 1.0;
// assign value to variable area
area = PI * radius * radius;
System.out.println( "The area of circle with radius " + radius + " is " + area );
}
}
The area of circle with radius 1.0 is 3.14159
如过将 area = PI * radius * radius;这条代码注释掉,则会出错
变量命名规范
变量作用域
public class VariableScope{
public static void main( String[] args ){
int x = 10;
{
int y = 20;
x++;
y++;
int x = 5;
}
x--;
y--;
}
}
VariableScope.java:8: 错误: 已在方法 main(String[])中定义了变量 x
int x = 5;
^
VariableScope.java:11: 错误: 找不到符号
y--;
^
符号: 变量 y
位置: 类 VariableScope
2 个错误
数据类型
boolean的值只能为:true或false
浮点型默认是double
浮点型默认是double,如果要转化为float,需要在数字后面加F。
float fff = 2.012;
不兼容的类型: 从double转换到float可能会有损失
float fff = 2.012;
^
1 个错误
正确写法
float fff = 2.012F;
float fff = 2.012f;
java的char类型占2Byte
一个字符可以表示一个汉字,与c有所不同
数据类型转换
短→长:可以自动转换
长→短:必须进行强制类型转换
上述代码均可自动转换
输入和输出
输入需要加载Scanner类
import java.util.Scanner;
Scanner input = new Scanner( System.in );
int n1 = input.nextInt();
float f1 = input.nextFloat();
String s1 = input.next();
Java Scanner 类 | 菜鸟教程 (runoob.com)
数组初始化
静态初始化与动态初始化不能混用
错误示例👇
import java.util.*;
public class Test{
public static void main(String[] args){
int[] score = new int[];
score[0] = 90;
score[1] = 92;
System.out.println( score[0] );
}
}
Test.java:4: 错误: 缺少数组维
int[] score = new int[];
^
1 个错误
int[] score = new int[4];
score = { 45, 87, 69, 98, 80 };
Test.java:5: 错误: 非法的表达式开始
score = { 45, 87, 69, 98, 80 };
^
Test.java:5: 错误: 不是语句
score = { 45, 87, 69, 98, 80 };
^
Test.java:5: 错误: 需要';'
score = { 45, 87, 69, 98, 80 };
^
3 个错误
数组的赋值
public class TestJava{
public static void main(String[] args){
int[] score2 = new int[]{ 60, 72, 90, 85, 100 };
int[] score4 = score2;
int[] score3 = score2;
score3[1] = 79;
System.out.println(score2[1]);
System.out.println(score3[1]);
System.out.println(score4[1]);
int[] arr = new int[3];
arr[0] = 10;
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
79
79
79
10
0
import java.util.*;
public class Test{
public static void main(String[] args){
int[] list1 = new int[10];
int[] list2 = new int[5];
for(int i = 0; i < list1.length; i++)
list1[i] = i;
list2 = list1;
System.out.println(list1);
System.out.println(list2);
for(int num: list1)
System.out.print(num + " ");
System.out.println();
for(int num: list2)
System.out.print(num + " ");
}
}
[I@279f2327
[I@279f2327
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
For-Each 循环
public class TestJava{
public static void main(String[] args){
int[] score = { 12, 32, 45, 48, 69 };
for ( int i = 0; i < score.length; i++ ) {
System.out.print(score[i] + " ");
}
System.out.println();
for( int num : score ) {
num++; //并不会改变原数组的值
System.out.print(num + " ");
}
System.out.println();
for( int i = 0; i < score.length; i++ ) {
System.out.print( score[i] + " ");
}
}
}
12 32 45 48 69
13 33 46 49 70
12 32 45 48 69
arraycopy方法
public class TestJava{
public static void main(String[] args){
int[] list1={34, 58, 69, 70, 23};
int[] list2 = new int[5];
// for(int i = 0; i < list1.length; i++)//数组拷贝方法一
// list2[i]=list1[i];
System.arraycopy(list1, 1, list2, 2, 2);//被拷贝数组 从1下标开始拷贝 拷贝到list2(从下标2开始存) 拷贝多长2
for(int num : list2)
System.out.println(num + " ");
System.out.println();
}
}
0
0
58
69
0
数组作为方法参数传递的是地址
arrays方法
Arrays (Java SE 17 & JDK 17) (oracle.com)
String类常用方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "Hello, world!";
String s2 = new String("Hello, world");
System.out.println(s1.length());
System.out.println(s2.length());
System.out.println(s1.charAt(0));
System.out.println(s2.charAt(1));
System.out.println("Hello, world!".length());
System.out.println("Hello ".trim());
System.out.println("Hello, ".concat("world!"));
String s3 = s1.toLowerCase();
System.out.println(s3);
System.out.println(s3.toUpperCase());
System.out.println(s3); //方法并不会改变原本内容
}
}
13
12
H
e
13
Hello
Hello, world!
hello, world!
HELLO, WORLD!
hello, world!
import java.util.*;
public class Main{
public static void main( String[] args ){
String str = "Welcome to Java";
System.out.println( "The length of " + str + " is " + str.length() );
int index = 0;
System.out.println( "The " + index + "th character of " + str + " is " + str.charAt(index) );
}
}
The length of Welcome to Java is 15
The 0th character of Welcome to Java is W
import java.util.*;
public class Main{
public static void main( String[] args ){
Scanner input = new Scanner(System.in);
System.out.println("Eenter a char: ");
String s = input.nextLine();
char c = s.charAt(0);
System.out.println(c);
}
}
Eenter a char:
asdfasd
a
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "Hello";
String s2 = "World";
String s3 = s1.concat(s2);
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("s3: " + s3);
}
}
s1: Hello
s2: World
s3: HelloWorld
String.trim()方法去头尾空格
import java.util.*;
public class Main{
public static void main( String[] args ){
String s3 = "\t Good Night \n"; //只能去头尾空白,中间不能去
System.out.println("s3 before: " + s3);
System.out.println(s3.trim());
System.out.println(s3); //本身并无变化
}
}
Good Night
Good Night
Good Night
split()方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String s4 = " H E L L O ";
String[] strs = s4.split( " " );
for( String str:strs )
System.out.print( str );
}
}
HELLO
String类常用方法二
String.equals()方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "Welcome";
String s2 = "Welcome";
String s3 = "welcome";
System.out.println( s1.equals( s2 ) );//equals()比较的是字符串在堆里面的内容
System.out.println( s1.equals( s3 ) );
System.out.println( s1.equalsIgnoreCase( s3 ) );
System.out.println( s1 == s2 );
System.out.println( s1 == s3 );
System.out.println( s2 == s3 );//==比较的是栈空间里字符串的地址
}
}
true
false
true
true
false
false
String.compareTo方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "Welcome";
String s2 = "Welcome";
String s3 = "welcome";
System.out.println( s1.compareTo( s2 ) );
System.out.println( s1.compareTo( s3 ) );
System.out.println( s1.compareToIgnoreCase( s3 ) );
}
}
0
-32
0
String.starstWith() String.endsWith ()
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "Welcome";
String s2 = "Welcome";
String s3 = "welcome";
System.out.println( s1.startsWith("We") );
System.out.println( s1.startsWith("we") );
System.out.println( s1.endsWith("me") );
}
}
true
false
true
String类常用方法三
String.substring( )方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String str = "Welcome to Java";
String substr1 = str.substring( 4 );
String substr2 = str.substring( 0, 11 ) + "HTML"; //左闭右开 从下标0到下标10
System.out.println( substr1 );
System.out.println( substr2 );
}
}
ome to Java
Welcome to HTML
indexOf()方法 和 lastIndexOf()
import java.util.*;
public class Main{
public static void main( String[] args ){
String str = "Welcome to Java";
System.out.println( str.indexOf('W') );//输出0
System.out.println( str.indexOf('o') );//输出4,从头往尾找到的第一个'o'的下标
System.out.println( str.indexOf('o',5) );//输出9,从下标为5开始找到的第一个'o'的下标
System.out.println( str.indexOf("come") );//输出3
System.out.println( str.indexOf("Java",5) );//输出11,从下标为5开始找
System.out.println( str.indexOf("java",5) );//输出-1,表明没有找到
System.out.println( str.lastIndexOf('W') );//输出0,从尾巴往头找到的第一个'W"的下标
System.out.println( str.lastIndexOf('o') );//输出9,从尾巴往头找到的第一个'o"的下标
System.out.println( str.lastIndexOf('o',5) );//输出4,从下标5开始尾巴往头找到的第一个'o"的下标
System.out.println( str.lastIndexOf("come") );//输出3
System.out.println( str.lastIndexOf("Java",5) );//输出-1没找到,从下标5开始尾巴往头找到的第一个'Java'的下标
System.out.println( str.lastIndexOf("java",5) );//输出-1没找到,从下标5开始尾巴往头找到的第一个'java'的下标
}
}
0
4
9
3
11
-1
0
9
4
3
-1
-1
Integer.parseInt方法
import java.util.*;
public class Main{
public static void main( String[] args ){
String s = "1234";
int n1 = Integer.parseInt( s );
int n2 = Integer.parseInt( s, 10 );
int n3 = Integer.parseInt( s, 16 );
int n4 = Integer.parseInt( "0101", 2 );
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
System.out.println(n4);
}
}
1234
1234
4660
5
装箱类
import java.util.*;
public class Main{
public static void main( String[] args ){
String s = "1234";
Integer n1 = Integer.valueOf( s );
Integer n2 = Integer.valueOf( s, 16 );
Integer n3 = Integer.valueOf( "0101", 2 );
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
}
}
1234
1234
4660
5
import java.util.*;
public class Main{
public static void main( String[] args ){
String s = Integer.toString( 1000 );
System.out.println(s);
}
}
1000
字符串创建机制
import java.util.*;
public class Main{
public static void main( String[] args ){
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println( "s1==s2?" + (s1==s2) );//指向同一块堆内存,结果为true
System.out.println( "s1==s3?" + (s1==s3) );//new 开辟了新空间,指向不同空间,地址不同,结果为false
System.out.println( "s1.equals(s2)?" + s1.equals(s2) );//内容相同,结果为true
System.out.println( "s1.equals(s3)?" + s1.equals(s3) );//内容相同,结果为true
s1 = "world";
System.out.println(s1==s2);//结果为false,指向了不同内容,即不同空间
}
}
s1==s2?true
s1==s3?false
s1.equals(s2)?true
s1.equals(s3)?true
false
StringBuffer类
import java.util.*;
public class Main{
public static void main( String[] args ){
StringBuffer sb = new StringBuffer( "Hello" );
int i = 101;
sb.append( " " ).append( "World" ).append( i ); //可任意添加
String str = sb.toString();
System.out.println( str );
}
}
Hello World101
java的一个源文件中,如果有多个类,这些类都没有用public修饰,那么文件名可以是任意一个类名。如果有public修饰,那么只能有一个类用public修饰,且文件名必须是这个public修饰的类名。
object类是所有类的父类,它是唯一一个没有父类的类
如果提供了带参数的构造方法,在构建对象时,不能通过没有参数的的构造函数构建,除非显示构建不带参数的构造函数。
this()方法指代默认的无参构造方法,其它构造方法可调用此方法,但该方法必须放在方法定义的第一行
public class TestHello{
String word;
String word2;
TestHello(){
word = "Hello";
word2 = "";
}
TestHello(String wd){
this();
word2 = wd;
}
public String getWord(){
return word + " " + word2;
}
public static void main(String[] args){
TestHello th = new TestHello("World");
System.out.println(th.getWord());
}
}
Hello World
同样this.()也可以指代带参数的构造方法
public class TestHello{
String word;
String word2;
TestHello(){
this("World");
word = "Hello";
}
TestHello(String wd){
word2 = wd;
}
public String getWord(){
return word + " " + word2;
}
public static void main(String[] args){
TestHello th = new TestHello();
System.out.println(th.getWord());
}
}