Java之基础1

有关java的相关知识点:

1、常量的声明:final int pi=3.1415;

2、输出:变量与字符字符串的连接用+;

例如:System.out.println(“this is pi’s number:”+pi);

3、字符串:

#### (1)、字符串的声明:String name="nuo cheng";

创建数组字符:char good[]={'a','v','f','g'};

等价于
String good=new String("avfg");

#### (2)、得出字符串的长度:
int len=good.length();

#### (3)、在字符串中指定索引位置的字符:
String str="nuo cheng";
char mychar=str.charAt(4);

#### (4)、从字符串中获取片段字符串:
String str="nuo cheng";
char mychar=str.substring(2);

#### (5)、去掉字符串中的头部和尾部的空格:

String str=" nuo cheng ";
System.out.println(" nuo cheng 去掉空格后的样式"+str.trim());

#### (6)、字符串中的字符替换:

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.imooc;
public class Helloworld{
static final int ip=3423;
static int age=3;
public static void main(String[] args) {
int n=30;
System.out.println("这是常量ip:"+ip);
System.out.println("------------------------");
int a=1000;
int b=456;
int result=b+a;
System.out.println("这里是输出的结果"+(int)result);
System.out.println("------------------------");
System.out.println("-------这是符合语句-------------");
int x=20;
{
int y=40;
System.out.println(y);
int z=45654;
boolean h;
{
h=y>z;
System.out.println(h);
}
}
String world="hello world";
System.out.println(world);
System.out.println("------------------------");
System.out.println("------字符串的声明String--------");
String name="诺铖";
System.out.println(name);
System.out.println("------创建String数组--------");
char good[]= {'a','b','c','d'};
String na=new String("dasf");
System.out.println(na);
int i;
for(i=0;i<4;i++) {
System.out.println(" "+good[i]);
}
String good1=new String("abcd");
String good2=new String("efghi");
System.out.println(good1+" "+good2);
int len=good1.length();
System.out.println(len);
System.out.println("------指定索引位置的字符--------");
String str="hello world";
char mychar = str.charAt(6);
System.out.println("从hello world中获取第6位的字符"+mychar);
System.out.println("------获取字符--------");
String str1="hello world";
String str2=str1.substring(5);
System.out.print(str2);
System.out.println("------去掉头部和尾部字符串中的空格--------");
String h=" nuo cheng ";
String g=h.trim();
System.out.println(" nuo cheng 字符串中去掉空格后的样式"+h.trim());
}
}

Fork me on GitHub