最近开始学习Java和C++,就将JAVA基础知识的小总结放在这里:
基础说明
1、在一个源程序中,只能有一个包声明语句。
2、原程序中可以有任意一个import引入语句。当源程序在编译时,会将需要的引入语句中的类引入到程序中,对不需要的就不会引入,而C/C++都包括进去。
3、类的体是由成员变量和成员方法等组成的。如成员变量x,y和成员方法main()构成类ClassName的体。(随便举个例子)
4、类的成员方法体现了类所定义类型的对象具有的行动。它的创建形式类似其他语言中的方法,它有名称、参数以及返回数据类型等。
5、在一个Java源程序中,如果有多个类创建,只有一个类可以被声明为public类。若某个类有main()方法,则声明该类为public类。
6、若想创建多个public类,就应该为每一个类单独地创建一个源程序。
7、应该用public修饰的类,即公共类作为原程序的文件名。
8、main()方法是一个特殊的方法,是Application程序运行的入口,它有相对固定的声明形式和规范,所以在声明创建main()方法的时候,必须遵守其固定格式:
public static void main( )(String args[ ])
** public 表示main()方法能被任何对象调用
** static 表示main()方法是一个静态方法或者说是一个类方法,它能够不创建类ClassName的实例而被直接调用
** void 表示main()方法结束返回时不返回任何值
9、Java字符集与C/C++等一些语言不同,它采用的是Unicode码,又称统一码字符集,由于它使用的是16位存储空间,能够支持多国语言,更具有国际化特性。当Unicode中的高8位全部为0时,则低8位的编码与ASCll码相同。
10、Java的数据类型分为简单类型和引用类型(=类+数组+接口)。前者被声明的时候,存储空间也同时被分配;后者声明变量时,是不会为这个变量分配存储空间,需要用new运算符来引用类型的变量分配储存空间。事实上,用类来声明的变量不是数据本身,而是数据的引用。
11、简单数据类型还有封装器(Wrapper)类。
12、PS:本人发现Java特别喜欢String和Character这种第一个大写后面小写的方式,所以发现问题的时候试试查查看是不是大小写的问题。
数组和字符串类的简单函数
1、输入:
首先 import java.util.Scanner
然后 创建实例对象:
Scanner input=new Scanner (System.in);
int a;
a=input.nextInt();
2、输出:
for each访问数组,但是不能修改【因为访问的方式是赋值给你创建的元素,修改的不是原元素】
但是用于遍历非常的方便
public class Helloworld {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n;
n=input.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=input.nextInt();
for(int element:a)
System.out.println(element);
n=input.nextInt();
System.out.print(a);
}
}
3、数组可以作为函数参数和返回值
public class Helloworld{
public static void main(String[] args) {
int []test= {1,2,4,5,7};//静态初始化
for(int i: test) {System.out.print(i+" ");}
System.out.println("\n");
test=Helloworld.reverse(test);//数组可以直接赋值
//但这里复制的仅仅是引用,两个数组指向同一个地址。
for(int i:test) {System.out.print(i+" ");}
}
public static int[] reverse(int []arr) {
int []result=new int[arr.length];
for(int i=0,j=result.length-1;i<arr.length;i++,j--) {
result[j]=arr[i];
}
return result;
}
}
4、二维数组允许每一维的大小不同
5、深入理解数组变量是引用变量
复制数组只是复制了引用,地址是相同的。
如果要进行深复制需要使用clone()或者单个赋值。
对于二维数组来说,直接clone数组是不行的,因为Java只有一维数组,二维数组不过是存放数组的引用的一维数组,直接clone是复制了每一个数组引用而已,需要对二维数组里的每一个引用即单个的一维数组进行clone。
package welcome;
import java.util.Scanner;
public class Helloworld{
public static void main(String[] args) {
int [][]test= {{1},{2},{4},{5},{7}};//静态初始化
for(int []arr:test)for(int i:arr) {System.out.print(i+" ");}/*FOR EACH遍历二维数组的方法*/
System.out.println("\n");
int [][]test2=test.clone();
test[0][0]=2;
for(int []arr:test2)for(int i:arr) {System.out.print(i+" ");}
}
}
输出:
1 2 4 5 7
2 2 4 5 7
这并不是我们要的深复制,所以再采用单个clone办法,复制每一个引用类型,这个类型里的都不是引用类型而是int了。
package welcome;
import java.util.Scanner;
public class Helloworld{
public static void main(String[] args) {
int [][]test= {{1},{2},{4},{5},{7}};//静态初始化
for(int []arr:test)for(int i:arr) {System.out.print(i+" ");}
System.out.println("\n");
int test2[][]=test.clone();//利用浅层复制分配新的引用存放地址
for(int i=0;i<test.length;i++)
test2[i]=test[i].clone();//深复制
test[0][0]=2;
for(int []arr:test2)for(int i:arr) {System.out.print(i+" ");}
}
}
最后对test改变值不影响test2的值,达到了深层复制的目的。
6、Java字符串是用类实现的,而C语言中使用字符数组实现的。一共有String和StringBuffer两个类,前者定长,后者变长。
声明:String str = new ("This is a String")或者="This is a String";或者=new String(字符数组);
数组的长度是数组的属性,而字符串的长度是String类的方法=str.length()。
7、Java输出格式化数字可以使用printf()和format()方法。
format()可以返回一个String对象,而前者返回一个PrintStream对象。
String fs = String.format("浮点型变量的值为 " + "%f, 整型变量的值为 " + " %d, 字符串变量的值为 " + " %s", floatVar, intVar, stringVar);
8、字符串可以通过“+”连接,基本数据类型与字符串进行“+”操作,也会自动转换为字符串。
public class Demo {
public static void main(String[] args){
String stuName = "小明";
int stuAge = 17;
float stuScore = 92.5f;
String info = stuName + "的年龄是 " + stuAge + ",成绩是 " + stuScore; System.out.println(info);
}
}
9、求字符串对象的相关信息。
如果想确定字符串中指定字符或子字符串在给定字符串的位置,可用 indexOf(subString[, startIndex])和lastIndexOf(subString)方法。例如:
① String str="ThisisaString";
② int index1 =str.indexOf("i"); //index=2
③int index2=str.indexOf("i",index1+1); //index2=4
④ int index3=str.lastIndexOf("i"); //index3=10
⑤ int index4=str.indexOf("String"); //index4=7
如果想要判断两个字符串是否相等,直接用equals()方法即可,返回一个Boolean值。
① String str="This is a String";
② Boolean result=str.equals("This is another String");
方法charAt()用以得到指定位置的字符。例如:
① String str="Thisis a String";
② char chr=str.charAt(3); //chr=s
方法getChars()用以得到字符串的一部分字符串。例如:
① public void getChars(int srcBegin,intsrcEnd,char[]dst,intdstBegin)
② String str="ThisisaString";
③ char chr=new char[10];
④ Str.getChars(5,12,chr,0);
⑤ System.out.println(new String(chr));
输出:saStrin
Substring 求子字符串
public String substring(int beginIndex, int endIndex)
第一个int为开始的索引,对应String数字中的开始位置,
第二个是截止的索引位置,对应String中的结束位置
取得的字符串长度为:endIndex - beginIndex;
从beginIndex开始取,到endIndex结束,从0开始数,其中不包括 endIndex位置的字符
① String str="ThisisaString";
② String str1=str.substring(1,4);
③ System.out.println(str1);
输出:his
10、字符串的操作
replace()方法可以将字符串中的一个字符替换为另一个字符。
① String str="ThisisaString";
② String str1=str.replace('T','t');//str1="thisisaString"
concat()方法可以把两个字符串合并为一个字符串。
① String str="Thisis a String";
② String str1=str.concat("Test"); //str1="Thisis a StringTest"
toUpperCase()和toLowerCase()方法分别实现字符串大小写的转换。
① String str="THISISASTRING";
② String str1=str.toLowerCase(); //str1="thisisastring";
trim()方法可以将字符串中开头和结尾处的空格去掉.
① String str=" This is a String ";
② String str1=str.trim(); //str1="This is a String"
String类提供静态方法valueOf(),它可以将任何类型的数据对象转换为一个字符串。如:
System.out.println( String.valueOf(Math.PI) );
以指定字符串作为分隔符,对当前字符串进行分割,分割的结 果存放在一个数组中。
import java.util.*;public class Demo {
public static void main(String[] args){
String str = “UESTC_is_good";
String strArr[] = str.split("_");
System.out.println(strArr[0]);
System.out.println(strArr[1]);
System.out.println(strArr[2]);
System.out.println(Arrays.toString(strArr));
}
}
输出结果:
UESTC
is
good
11、修改可变字符串
StringBuffer类为可变字符串的修改提供了3种方法,在字符串中间插入 和改变某个位置所在的字符。
① 在字符串后面追加:用append()方法将各种对象加入到字符串中。
② 在字符串中间插入:用insert()方法。
③ 改变某个位置所在的字符,用setCharAt()方法。
StringBuffer str=new StringBuffer("Thisis a String");
str.insert(9,"test ");
System.out.println(str.toString()); //输出: Thisis a test String
StringBuffer sb =new StringBuffer("aaaaaa");
sb.setCharAt(2,‘b’);// sb 的值 aabaaa
System.out.println(sb);
public class TestString{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer(“电子科技大学官网:");
sBuffer.append("www");
sBuffer.append(".uestc");
sBuffer.append(".edu");
sBuffer.append(".cn");
System.out.println(sBuffer); }
}
String类加需要重新new一下对象,而StringBuffer直接加上去,速度快了很多倍
12、Java的日期与时间
java.util 包提供了 Date 类来封装当前的日期和时间。
Date 类提供两个构造函数来实例化 Date 对象。
第一个构造函数使用当前日期和时间来初始化对象。Date()
第二个构造函数接收一个参数,该参数是从1970年1月1日起 的毫秒数。Date(long millisec)
Java中获取当前日期和时间很简单,使用 Date 对象的 toString() 方法来打印当前日期和时间.
import java.util.Date;
public class DateDemo {
public static void main(String args[]) { // 初始化 Date 对象
Date date = new Date(); // 使用 toString() 函数显示日期时间
System.out.println(date.toString());
}
}
Simpledateformate:
SimpleDateFormat 是一个以语言环境敏感的方式来格式化和分析 日期的类。SimpleDateFormat 允许选择任何自定义日期时间格式.
单引号包括的不会被解释,其余都会当成模式字母解释。
详情请见:https://blog.csdn.net/qq_27093465/article/details/53034427
使用printf格式化日期:
转换符 | 说明 | 示例 |
c | 包括全部日期和时间信息 | 星期一 九月 18 14:21:20 CST 2017 |
F | "年-月-日"格式 | 2017-09-18 |
D | "月/日/年"格式 | 09/18/17 |
r | "HH:MM:SS PM"格式12时制 | 02:25:51 下午 |
T | "HH:MM:SS"格式24时制 | 14:28:16 |
R | "HH:MM"格式24时制 | 14:28 |
import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
Date date = new Date(); // 初始化 Date 对象
System.out.printf("全部日期和时间信息: %tc%n",date); //c的使用
System.out.printf("年-月-日格式: %tF%n",date); //f的使用
System.out.printf("月/日/年格式: %tD%n",date); //d的使用
System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date); //r的使用
System.out.printf("HH:MM:SS格式(24时制): %tT%n",date); //t的使用
System.out.printf("HH:MM格式(24时制): %tR",date); //R的使用
}
}
全部日期和时间信息: Mon Sep 18 21:20:06 CST 2017
年-月-日格式: 2017-09-18
月/日/年格式: 09/18/17
HH:MM:SS PM格式(12时制):09:20:06 PM
HH:MM:SS格式(24时制): 21:20:06
HH:MM格式(24时制): 21:20