java基础篇
IDEA常用快捷键
快捷键功能
Alt+Enter自动修正代码Ctrl+Y删除光标所在行Ctrl+D复制光标所在行的内容,插入光标位置下面Ctrl+Alt +L格式化代码Ctrl+/单行注释,再按取消注释Ctrl+Shift+/选中代码注释,多行注释,再按取消注释。Alt+Ins自动生成代码,toString,get,set等方法Alt+Shift+上下箭头移动当前代码行
字符串概述和特点
字符串的构造方法和直接创建
package cn
.itcast
.day01
.demo01
.String
;
public class Demo01String {
public static void main(String
[] args
) {
String str1
= new String();
System
.out
.println("第一个字符串:" + str1
);
char[] charArray
= {'A', 'B', 'C'};
String str2
= new String(charArray
);
System
.out
.println("第二个字符串:" + str2
);
byte[] byteArray
= {97,98,99};
String str3
= new String(byteArray
);
System
.out
.println("第三个字符串:" + str3
);
String str4
= "Hello";
System
.out
.println("第四个字符串:" + str4
);
}
}
字符串的常量池
package cn
.itcast
.day01
.demo01
.String
;
public class Demo02StringPool {
public static void main(String
[] args
) {
String str1
= "abc";
String str2
= "abc";
char[] charArray
= {'a','b','c'};
String str3
= new String(charArray
);
System
.out
.println(str1
== str2
);
System
.out
.println(str1
== str3
);
System
.out
.println(str2
== str3
);
}
}
字符串的比较相关方法
package cn
.itcast
.day01
.demo01
.String
;
public class Demo01StringEquals {
public static void main(String
[] args
) {
String str1
= "Hello";
String str2
= "Hello";
char[] charArray
= {'H','e','l','l','o'};
String str3
= new String(charArray
);
System
.out
.println(str1
.equals(str2
));
System
.out
.println(str2
.equals(str3
));
System
.out
.println(str3
.equals("Hello"));
System
.out
.println("Hello".equals(str1
));
String str4
= "Hello";
System
.out
.println(str1
.equals(str4
));
System
.out
.println("==================");
String str5
= null
;
System
.out
.println("abc".equals(str5
));
System
.out
.println("==============================");
String strA
= "Java";
String strB
= "java";
System
.out
.println(strA
.equals(strB
));
System
.out
.println(strA
.equalsIgnoreCase(strB
));
System
.out
.println("abc一123".equalsIgnoreCase("abc壹123"));
}
}
字符串的获取相关方法
package cn
.itcast
.day01
.demo01
.String
;
public class Demo01StringGet {
public static void main(String
[] args
) {
int length
= "ajdfkasjdf".length();
System
.out
.println("字符串的长度是:" + length
);
String str1
= "Hello";
String str2
= "World";
String str3
= str1
.concat(str2
);
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println(str3
);
System
.out
.println("============");
char ch
= "Hello".charAt(1);
System
.out
.println("在1号索引位置的字符是:" + ch
);
String original
= "HelloWorldHelloWorld";
int index
= original
.indexOf("llo");
System
.out
.println("第一次索引值是:" + index
);
System
.out
.println("HelloWorld".indexOf("abc"));
}
}
字符串的截取方法
package cn
.itcast
.day01
.demo01
.String
;
public class Demo01SubString {
public static void main(String
[] args
) {
String str1
= "HelloWorld";
String str2
= str1
.substring(5);
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println("========================");
String str3
= str1
.substring(4,7);
System
.out
.println(str3
);
System
.out
.println("========================");
String strA
= "Hello";
System
.out
.println(strA
);
strA
= "Java";
System
.out
.println(strA
);
}
}
字符串的转换相关方法
package cn
.itcast
.day01
.demo01
.String
;
public class Demo01StringConvert {
public static void main(String
[] args
) {
char[] chars
= "Hello".toCharArray();
System
.out
.println(chars
[0]);
System
.out
.println(chars
.length
);
System
.out
.println("=====");
byte[] bytes
= "abc".getBytes();
for (int i
= 0; i
< bytes
.length
; i
++) {
System
.out
.println(bytes
[i
]);
}
System
.out
.println("=====");
String str1
= "How do you do?";
String str2
= str1
.replace("o","*");
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println("=====");
}
}
字符串的分割方法
package String
;
public class Demo05StringSplit {
public static void main(String
[] args
) {
String str1
= "abc,bbb,dnf";
String
[] array1
= str1
.split(",");
for (int i
= 0; i
< array1
.length
; i
++) {
System
.out
.println(array1
[i
]);
}
System
.out
.println("=====");
String str2
= "AAA BBB DDD";
String
[] array2
= str2
.split(" ");
for (int i
= 0; i
< array2
.length
; i
++) {
System
.out
.println(array2
[i
]);
}
System
.out
.println("=====");
String str3
= "DDD.NNN.FFF";
String
[] array3
= str3
.split("\\.");
for (int i
= 0; i
< array3
.length
; i
++) {
System
.out
.println(array3
[i
]);
}
}
}
练习:按指定格式拼接字符串
package String
;
public class Demo06StringParams {
public static void main(String
[] args
) {
int[] array
= {1,2,3};
String result
= fromArrayToString(array
);
System
.out
.println(result
);
}
public static String
fromArrayToString(int[] array
) {
String str
= "[";
for (int i
= 0; i
< array
.length
; i
++) {
if (i
== array
.length
- 1) {
str
+= "word" + array
[i
] + "]";
}else {
str
+= "word" + array
[i
] + "#";
}
}
return str
;
}
}
练习:统计输入的字符串中各种字符的个数
package String
;
import java
.util
.Scanner
;
public class Demo07StringCount {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入一个字符串:");
String input
= sc
.next();
int countUpper
= 0;
int countLower
= 0;
int countNumber
= 0;
int countOther
= 0;
char[] charArray
= input
.toCharArray();
for (int i
= 0; i
< charArray
.length
; i
++) {
char ch
= charArray
[i
];
if('A' <= ch
&& ch
<= 'Z') {
countUpper
++;
} else if ('a' <= ch
&& ch
<= 'z') {
countLower
++;
} else if ('0' <= ch
&& ch
<= '9') {
countNumber
++;
} else {
countOther
++;
}
}
System
.out
.println("大写字母有:" + countUpper
);
System
.out
.println("小写字母有:" + countLower
);
System
.out
.println("数字有:" + countNumber
);
System
.out
.println("其他字符有:" + countUpper
);
}
}
静态static关键字概述
静态static关键字修饰成员变量
package Static
;
public class Student {
private String name
;
private int age
;
static String room
;
private static int idCounter
= 0;
public Student() {
this.idCounter
= ++idCounter
;
}
public Student(String name
, int age
) {
this.name
= name
;
this.age
= age
;
this.idCounter
= ++idCounter
;
}
public static int getIdCounter() {
return idCounter
;
}
public static void setIdCounter(int idCounter
) {
Student
.idCounter
= idCounter
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
}
package Static
;
public class Demo01Static {
public static void main(String
[] args
) {
Student one
= new Student("吴奇隆",19);
one
.room
= "101教室";
System
.out
.println("姓名:" + one
.getName() + ",年龄:" + one
.getAge() + ",教室:" + one
.room
+ ",学号:" + one
.getIdCounter());
Student two
= new Student("刘诗诗",18);
System
.out
.println("姓名:" + two
.getName() + ",年龄:" + two
.getAge() + ",教室:" + two
.room
+ ",学号:" + two
.getIdCounter());
}
}
静态static关键字修饰成员方法
package Static
;
public class MyClass {
int num
;
static int numStatic
;
public void method(){
System
.out
.println("这是一个成员方法");
System
.out
.println(num
);
System
.out
.println(numStatic
);
}
public static void methodStatic(){
System
.out
.println("这是一个静态方法");
System
.out
.println(numStatic
);
}
}
package Static
;
public class Demo02StaticMethod {
public static void main(String
[] args
) {
MyClass obj
= new MyClass();
obj
.method();
obj
.methodStatic();
MyClass
.methodStatic();
myMethod();
Demo02StaticMethod
.myMethod();
}
public static void myMethod() {
System
.out
.println("自己的方法!");
}
}
静态static的内存图
静态代码块
package Static
;
public class Person {
static {
System
.out
.println("静态代码块");
}
public Person() {
System
.out
.println("动态代码块");
}
}
package Static
;
public class Demo04StaticPerson {
public static void main(String
[] args
) {
Person one
= new Person();
Person two
= new Person();
}
}
数组工具类arrays
package Array
;
import java
.util
.Arrays
;
public class Demo01Arrays {
public static void main(String
[] args
) {
int[] intArray
= {10,20,30};
String intStr
= Arrays
.toString(intArray
);
System
.out
.println(intStr
);
int[] array1
= {2,1,3,10,6};
Arrays
.sort(array1
);
System
.out
.println(Arrays
.toString(array1
));
String
[] array2
= {"bbb","ccc","aaa"};
Arrays
.sort(array2
);
System
.out
.println(Arrays
.toString(array2
));
}
}
arrays练习:字符串倒序排列
package Array
;
import java
.util
.Arrays
;
public class ArraysTest {
public static void main(String
[] args
) {
String str
= "ysUreadfSRWEJDSFJJ123";
char[] chars
= str
.toCharArray();
Arrays
.sort(chars
);
for (int i
= chars
.length
- 1; i
>= 0; i
--) {
System
.out
.print(chars
[i
]);
}
}
}
数学工具类Math
package Math
;
public class Demo01Math {
public static void main(String
[] args
) {
System
.out
.println(Math
.abs(3.14));
System
.out
.println(Math
.abs(-3.14));
System
.out
.println(Math
.abs(0));
System
.out
.println("=========");
System
.out
.println(Math
.ceil(3.9));
System
.out
.println(Math
.ceil(-3.1));
System
.out
.println(Math
.ceil(3.1));
System
.out
.println(Math
.ceil(3.0));
System
.out
.println("=========");
System
.out
.println(Math
.floor(30.1));
System
.out
.println(Math
.floor(-30.9));
System
.out
.println(Math
.floor(31.0));
System
.out
.println("=========");
System
.out
.println(Math
.round(20.4));
System
.out
.println(Math
.round(10.5));
System
.out
.println("=========");
}
}
练习题:小学模考真题
package Math
;
public class Demo01MathPractise {
public static void main(String
[] args
) {
int count
= 0;
double min
= -10.8;
double max
= 5.9;
for(int i
= (int) min
;i
< max
;i
++) {
int abs
= Math
.abs(i
);
if(abs
> 6 || abs
< 2.1) {
System
.out
.println(i
);
count
++;
}
}
System
.out
.println("总共有:" + count
);
}
}
转载请注明原文地址:https://tech.qufami.com/read-10493.html