idea
psvm
public static void main ( String [ ] args) {
}
sout
System . out. println ( "" ) ;
注释
public class helloworld {
public static void main ( String [ ] args) {
System . out. println ( "hello world" ) ;
}
}
javaDoc
参数信息
@author 作者名
@version 版本号
@since 指明需要最早使用的jdk版本
@param 参数名
@return 返回值情况
@throws 异常抛出情况
基本类型
public class helloworld {
public static void main ( String [ ] args) {
int num1= 10 ;
0 为八进制
0 x为十六进制
byte num2 = 10 ;
short num3= 10 ;
long num4 = 20l ;
float num5 = 20.2F ;
double num6 = 3.14159265358 ;
char name = '国' ;
System . out. print ( ( int ) name) ;
String name1 = "hello" ;
boolean flag = true ;
boolean go = false ;
}
}
类型转换
public class helloworld {
public static void main ( String [ ] args) {
int i = 128 ;
byte b = ( byte ) i;
System . out. println ( i) ;
System . out. println ( b) ;
}
}
128
- 128
——————————————————————————————————————————————————————————————————————————————————
public class helloworld {
public static void main ( String [ ] args) {
int i = 128 ;
double b = i;
System . out. println ( i) ;
System . out. println ( b) ;
}
}
128
128.0
————————————————————————————————————————————————————————————————————————————————————
char c = 'a' ;
int m= c+ 1 ;
System . out. println ( m) ;
System . out. println ( ( char ) m) ;
98
b
————————————————————————————————————————————————————————————————————————————————————
int money = 10_0000_0000 ;
int years = 20 ;
int total = money* years;
long total2 = money* years;
long total3 = money* ( ( long ) years) ;
System . out. println ( total3) ;
变量
type varName [ = value] [ , varName [ = value] ]
public class helloworld {
String name;
int age;
static double salary = 250 ;
public static void main ( String [ ] args) {
int i = 1 ;
helloworld helloworld= new helloworld ( ) ;
System . out. println ( helloworld. age) ;
System . out. println ( salary) ;
}
public void add ( ) {
System . out. println ( ) ;
}
}
* * 常量* *
static final double PI = 3.14 ;
scanner
package base ;
import java. util. Scanner ;
public class HelloWorld {
public static void main ( String [ ] args) {
Scanner scanner= new Scanner ( System . in) ;
System . out. printf ( "使用next方式" ) ;
if ( scanner. hasNext ( ) ) {
String srt = scanner. next ( ) ;
System . out. printf ( "输出的内容为" + srt) ;
}
scanner. close ( ) ;
}
}
next ( ) :
1 、一定要读取到有效字符后才可以结束输入。
2 、对输入有效字符之前遇到的空白,next ( ) 方法会自动将其去掉。
3 、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
4 、next ( ) 不能得到带有空格的字符串。
nextLine ( ) :
1 、以Enter 为结束符, 也就是说nextLine ( ) 方法返回的是输入回车之前的所有字符。
2 、可以获得空白。
* * package base ;
import java. util. Scanner ;
public class HelloWorld {
public static void main ( String [ ] args) {
int i = 0 ;
float f = 0.1f ;
Scanner scanner= new Scanner ( System . in) ;
System . out. printf ( "使用next方式" ) ;
if ( scanner. hasNextInt ( ) ) {
i = scanner. nextInt ( ) ;
System . out. printf ( "输出的内容为" + i) ;
} else {
System . out. printf ( "输入的不是整数" ) ;
}
System . out. printf ( "请输入小数" ) ;
if ( scanner. hasNextFloat ( ) ) {
f = scanner. nextFloat ( ) ;
System . out. printf ( "输出的内容为" + f) ;
} else {
System . out. printf ( "输入的不是小数" ) ;
}
scanner. close ( ) ;
}
}
while ( scanner. hasNextDouble ( ) ) {
} * *
if条件选择
单选择
if ( 布尔表达式) {
}
——————————————————————————————————————————————————————————————————————————————
双选择
if ( 布尔表达式) {
} else {
}
——————————————————————————————————————————————————————————————————————————————
if 多选择结构
if ( 布尔表达式1 ) {
/ 如果布尔表达式1 的值为true 执行代码
} else if ( 布尔表达式2 ) {
} else if ( 布尔表达式3 ) {
} else {
}
——————————————————————————————————————————————————————————————————————————————
if 嵌套
if ( 布尔表达式1 ) {
if ( 布尔表达式2 ) {
}
}
switch选择法
switch ( expression) {
case value :
break ;
case value :
break ;
default :
}
while循环
while ( 布尔表达式) {
}
public class HelloWorld {
public static void main ( String [ ] args) {
int i = 0 ;
while ( i< 100 ) {
i++ ;
System . out. println ( i) ;
}
}
}
————————————————————————————————————————————————————————————————————————————————————————
do {
} while ( 布尔表达式) ;
for循环
for ( 初始化; 布尔表达式; 更新) {
}
100. for
for ( int i= 1 ; i<= 100 ; i++ ) {
System . out. println ( i) ;
}
== == == == == == == == == == == == == == == == ==
增强型循环
for ( int array : arrays) {
System . out. print ( array) ;
}
数组
int [ ] ints;
int inst[ ] ;
ints = new int [ 10 ] ;
int [ ] gou = new int [ 10 ] ;
== == == == == == == == == == == == == == ==
写一个方法
/ 打印数组元素
public static void printArray ( int [ ] arrays) i
for ( int i = 0 ; i < arrays. length; i++ ) {
system. out. print ( arrays[ i] + ”") ;
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
二维数组
int [ ] [ ] array = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } ;
方法 oop
public class Demo01 {
public static void main ( String [ ] args) {
new Demo01 ( ) . sayHello ( ) ;
new Demo01 ( ) . max ( 2 , 3 ) ;
}
public static String sayHello ( ) {
System . out. println ( "hello world" ) ;
return null ;
}
public int max ( int a, int b) {
return a> b ? a : b ;
}
}
————————————————————————————————————————————————————————————————————————
static 的method可以直接调用
kind variable = new kind;
类和对象
public class Demo01 {
String name;
int age;
public void study ( ) {
System . out. println ( this . name+ "is study" ) ;
}
}
——————————————————————————
public class Demo02 {
public static void main ( String [ ] args) {
Demo01 xiao = new Demo01 ( ) ;
xiao. name= "inst" ;
xiao. age= 2 ;
}
}
异常Exception处理
异常处理的五个关键字
try , catch , finally , throw , throws
多个异常从小到大的捕获
________________________________________
public class Demo01 {
public static void main ( String [ ] args) {
int a = 1 ;
int b = 0 ;
try {
System . out. println ( a/ b) ;
} catch ( ArithmeticException e) {
System . out. println ( "除数不能为零" ) ;
} catch {
可以写多个异常
} finally {
System . out. println ( "finally" ) ;
用于关闭io流、资源
}
if ( b== 0 ) {
throw new ArithmeticException ( ) ;
}
}
public void a ( ) throws ArithmeticException {
{
int c = 0 ;
if ( c == 0 ) {
throw new ArithmeticException ( ) ;
}
}
;
}
}
_________
public class Demo02 extends Exception {
private int detail;
public Demo02 ( int a) {
this . detail = a;
}
@Override
public String toString ( ) {
return "Demo02{" +
"detail=" + detail +
'}' ;
}
}
public class Demo01 {
static void trysing ( int a) throws Demo02 {
System . out. println ( "cuangdi:" + a) ;
if ( a> 10 ) {
throw new Demo02 ( a) ;
}
System . out. println ( "ok" ) ;
}
public static void main ( String [ ] args) {
try {
trysing ( 1 ) ;
} catch ( Demo02 e) {
System . out. println ( "Demo02" + e) ;
}
}
}
常用类
public class Demo02 {
public static void main ( String [ ] args) {
Anima a = new Anima ( ) ;
Anima b = new Anima ( ) ;
System . out. println ( a. hashCode ( ) ) ;
System . out. println ( b. hashCode ( ) ) ;
}
}
class Anima {
private String name;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
public class Demo02 {
public static void main ( String [ ] args) {
Anima a = new Anima ( ) ;
Anima b = new Anima ( ) ;
a. setName ( "xiaohua" ) ;
b. setName ( "xiaohua" ) ;
a. setAge ( 1 ) ;
b. setAge ( 1 ) ;
a. setId ( "11101" ) ;
b. setId ( "11101" ) ;
System . out. println ( a. equals ( b) ) ;
System . out. println ( b. equals ( a) ) ;
}
}
class Anima {
private String name;
private int age;
private String id;
public void setAge ( int age) {
this . age = age;
}
public void setId ( String id) {
this . id = id;
}
public int getAge ( ) {
return age;
}
public String getId ( ) {
return id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
-- -- -- -- -- -- -- -- -- -- -- -
输入的是对象的属性
@Override
public String toString ( ) {
return "Anima [age=" + age + ", id=" + id + ", name=" + name + "]" ;
}
-- -- -- -- -- -- -- -- -- -- -
String 字符串常用方法
-- - boolean
equals ( anObject)
equalsIgnoreCase ( anotherString)
contains ( s)
startsWith ( prefix)
endsWith ( suffix)
isEmpty ( )
charAt ( index)
length ( )
indexOf ( str, 第n个索引开始)
lastIndexOf ( str)
substring ( beginIndex, endIndex)
getBytes ( StandardCharsets . *[ 指定字符集] ) ;
byte [ ] str1 = gout. getBytes ( StandardCharsets . US_ASCII) ;
String name1= new String ( str1, StandardCharsets . US_ASCII) ;
toCharArray ( ) ; / 将字符串转为字符数组
valueOf ( obj) ;
toLowerCase ( )
toUpperCase ( )
concat ( str) ;
replace ( target, replacement)
split ( “- ”)
trim ( ) ;
compareTo ( anotherString) ;
compareToIgnoreCase ( str) ;
String Buffer
append ( "" ) ;
insert ( 1 ,“”) ;
delete ( 1 , 2 ) ;
reverse ( ) ;
Math
Math . abs ( - 10 ) ;
Math . ceil ( 10.1 ) ;
Math . round ( 10.2 ) ;
Math . max ( 1 , 2 ) ;
Math . pow ( 10 , 2 ) ;
Math . random ( ) ;
System
System . currentTimeMillis ( ) ;
System . exit ( 1 ) ;
System . arraycopy ( src, srcPos, dest, destPos, length) ;
Data
SimpleDateFormat date1 = new SimpleDateFormat ( ) ;
String pattern = "yyyy-MM-dd HH:mm:ss" ;
Date date = date1. parse ( "" ) ;
Date date2 = new Date ( ) ;
String format = date1. format ( date2. getTime ( ) ) ;
G 年代标志符
y 年
M 月
d 日
h 时 在上午或下午 ( 1 ~ 12 )
H 时 在一天中 ( 0 ~ 23 )
m 分
s 秒
S 毫秒
E 星期
D 一年中的第几天
F 一月中第几个星期几
w 一年中第几个星期
W 一月中第几个星期
a 上午 / 下午 标记符
k 时 在一天中 ( 1 ~ 24 )
K 时 在上午或下午 ( 0 ~ 11 )
z 时区
-- -- -- -- -- -- -- -- -- --
Calender 对象控制
Calendar calendar = Calendar . getInstance ( ) ;
Date time = calendar. getTime ( ) ;
System . out. println ( time) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yy-MM-dd HH:mm:ss" ) ;
System . out. println ( sdf. format ( time) ) ;
calendar. add ( Calendar . DAY_OF_MONTH, - 3 ) ;
calendar. set ( Calendar . YEAR, 1925 ) ;
Date time1 = calendar. getTime ( ) ;
System . out. println ( time1) ;
正则表达式类
String str = "zifuchaung" ;
str1= "\\d{3}"
Pattern pattern = Pattern . compile ( str1) ;
Matcher matcher = pattern. matcher ( str) ;
boolean a = matcher. matches ( ) ;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
boolean b = Pattern . matches ( "zifuchaung" , str) ;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
boolean matches = str. matches ( "zifuchaung" ) ;
正则表达式语法
Untitled
集合
Collection
___________________
Collection col1 = new ArrayList ( ) ;
Collection col2 = new ArrayList ( ) ;
col2. addAll ( col1) ;
col1. add ( 12 ) ;
System . out. println ( col1) ;
col1. remove ( 10 ) ;
col1. removeAll ( col2) ;
boolean ant1 = col1. retainAll ( col2) ;
int size = col1. size ( ) ;
boolean contains = col1. contains ( "12" ) ;
boolean containsAll= col1. containsAll ( col2) ;
List
————————————————————————————
Vector list = new Vector ( ) ;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
List list1 = new ArrayList ( ) ;
List newlist = new ArrayList ( ) ;
list1. add ( 1 ) ;
list1. add ( 2 ) ;
list1. add ( 2 ) ;
list1. add ( 2 ) ;
list1. add ( 3 ) ;
System . out. println ( list1) ;
Iterator iterator = list1. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
int next = ( int ) iterator. next ( ) ;
if ( ! newlist. contains ( next) ) {
newlist. add ( next) ;
}
}
System . out. println ( newlist) ;
}
_________________________________
Set
HashSet
LinkedHashSet
TreeSet
________________________________
Set set1 = new HashSet ( ) ;
set1. add ( "a" ) ;
set1. add ( 1 ) ;
set1. add ( "c" ) ;
Object [ ] array = set1. toArray ( ) ;
System . out. println ( Arrays . toString ( array) ) ;
set1. remove ( "a" ) ;
import java. util. HashSet ;
import java. util. Set ;
public class demo {
public static void main ( String [ ] args) {
Set set1 = new HashSet ( ) ;
Teacter t1 = new Teacter ( ) ;
Teacter t2 = new Teacter ( ) ;
t1. setAge ( 22 ) ;
t2. setAge ( 33 ) ;
t1. setName ( "jian" ) ;
t2. setName ( "hua" ) ;
set1. add ( t1) ;
set1. add ( t2) ;
System . out. println ( set1) ;
}
}
class Teacter {
private String name;
private int age;
@Override
public String toString ( ) {
return "Teacter [name=" + name + ", age=" + age + "]" ;
}
public void setName ( String name) {
this . name = name;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public int getAge ( ) {
return age;
}
}
-- -- -- -- -- -- -- -- -
c为集合,elements可以添加多个元素用, 号分开
Collections . addAll ( c, elements) ;
Collections . sort ( list) ;
Collections . binarySearch ( list, key) ;
Collections . swap ( list, i, j) ;
Collections . reverse ( list) ;
Collections . replaceAll ( list, oldVal, newVal) ;
package map ;
import java. util. HashMap ;
import java. util. Iterator ;
import java. util. Map ;
import java. util. Set ;
import java. util. Map. Entry ;
public class demo {
public static void main ( String [ ] args) {
Map map = new HashMap ( ) ;
map. put ( 1001 , "value1" ) ;
map. put ( 1002 , "value2" ) ;
System . out. println ( map) ;
Object sum = map. get ( 1002 ) ;
System . out. println ( sum) ;
System . out. println ( "----------------------------" ) ;
Set entrySet = map. entrySet ( ) ;
for ( Object obj : entrySet) {
if ( obj instanceof Map. Entry ) {
Map. Entry entry = ( Map. Entry ) obj;
Object key = entry. getKey ( ) ;
Object value = entry. getValue ( ) ;
System . out. println ( key+ "=" + value) ;
}
}
System . out. println ( "----------------------------" ) ;
Set keySet = map. keySet ( ) ;
Iterator iterator = keySet. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
Object key = iterator. next ( ) ;
Object value = map. get ( key) ;
System . out. println ( key+ "=" + value) ;
}
}
}
-- -- -- -- -- -- -- --
HashMap map = new HashMap ( ) ;
map. put ( 0001 , "value" ) ;
map. put ( 0002 , "value" ) ;
map. put ( 0003 , "value" ) ;
map. put ( 0004 , "value" ) ;
map. put ( 0005 , "value" ) ;
Collection values = map. values ( ) ;
for ( Object object : values) {
System . out. println ( object) ;
}
IO流
package file ;
import java. io. File ;
import java. io. IOException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
import java. util. Arrays ;
public class fileio {
public static void main ( String [ ] args) {
File file = new File ( "/home/yan/IdeaProjects/javaSE" ) ;
boolean exists = file. exists ( ) ;
System . out. println ( exists) ;
try {
file. createNewFile ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
boolean file2 = file. isFile ( ) ;
boolean directory = file. isDirectory ( ) ;
file. delete ( ) ;
String [ ] list = file. list ( ) ;
System . out. println ( Arrays . toString ( list) ) ;
File [ ] listFiles = file. listFiles ( ) ;
for ( File f : listFiles) {
System . out. println ( "---------------------" ) ;
System . out. println ( f. getName ( ) ) ;
System . out. println ( f. getAbsolutePath ( ) ) ;
System . out. println ( f. getPath ( ) ) ;
System . out. println ( f. isHidden ( ) ) ;
System . out. println ( f. canRead ( ) ) ;
System . out. println ( f. lastModified ( ) ) ;
Date time = new Date ( f. lastModified ( ) ) ;
SimpleDateFormat df = new SimpleDateFormat ( "HH:mm:ss" ) ;
System . out. println ( df. format ( time) ) ;
}
File f1 = new File ( "/home/yan/IdeaProjects/javaSE/into" ) ;
f1. mkdir ( ) ;
f1. renameTo ( new File ( "/home/yan/IdeaProjects/javaSE/dest" ) ) ;
}
}
-- -- --
package file ;
import java. io. File ;
public class demo {
public static void main ( String [ ] args) {
findFile ( new File ( "/home/yan/Documents" ) , ".pdf" ) ;
}
private static void findFile ( File file, String ext) {
if ( file== null ) return ;
if ( file. isDirectory ( ) ) {
File [ ] files = file. listFiles ( ) ;
if ( files!= null ) {
for ( File f : files) {
findFile ( f, ext) ;
}
}
} else {
String name = file. getName ( ) . toLowerCase ( ) ;
if ( name. endsWith ( ext) ) {
System . out. println ( file. getAbsolutePath ( ) ) ;
}
}
}
}
-- -- -- -
package file ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. InputStream ;
import java. io. OutputStream ;
public class bytedemo {
public static void main ( String [ ] args) {
in ( ) ;
}
private static void out ( ) {
File f = new File ( "/home/yan/IdeaProjects/javaSE/test.txt" ) ;
try {
OutputStream out = new FileOutputStream ( f, true ) ;
String info = "my fuck your is father!\r" ;
out. write ( info. getBytes ( ) ) ;
out. close ( ) ;
System . out. println ( "success" ) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private static void in ( ) {
File f1 = new File ( "/home/yan/IdeaProjects/javaSE/test.txt" ) ;
try {
InputStream in = new FileInputStream ( f1) ;
byte [ ] bytes = new byte [ 2 ] ;
StringBuilder buf = new StringBuilder ( ) ;
int len = - 1 ;
while ( ( len= in. read ( bytes) ) != - 1 ) {
buf. append ( new String ( bytes, 0 , len) ) ;
}
System . out. println ( buf) ;
in. close ( ) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
-- -- -- -- --
package file ;
import java. io. File ;
import java. io. FileNotFoundException ;
import java. io. FileReader ;
import java. io. FileWriter ;
import java. io. IOException ;
import java. io. Reader ;
import java. io. Writer ;
public class charstream {
public static void main ( String [ ] args) {
out ( ) ;
in ( ) ;
}
private static void out ( ) {
File f = new File ( "/home/yan/IdeaProjects/javaSE/src/init.sh" ) ;
try {
Writer out = new FileWriter ( f, true ) ;
out. write ( "小河流水哗啦啦!\r" ) ;
out. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private static void in ( ) {
File f = new File ( "/home/yan/IdeaProjects/javaSE/src/init.sh" ) ;
try {
Reader in = new FileReader ( f) ;
char [ ] cs = new char [ 1 ] ;
int len = - 1 ;
StringBuilder str = new StringBuilder ( ) ;
while ( ( len= in. read ( cs) ) != - 1 ) {
str. append ( new String ( cs, 0 , len) ) ;
}
in. close ( ) ;
System . out. println ( str) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}