1.明确包装类型与基本数据类型的区别。
(1)Java 提供两种不同的类型:基本类型(内置类型)和引用类型(封装类型)。
基本类型: boolean char byte short int long float double
封装类型 :Boolean Character Byte Short Integer Long Float Double
(2)int是基本类型,直接存数值。如:int i = 5;
Integer是封装类型,产生对象时用一个引用指向这个对象。如:Integer i = new Integr(5)
(3)Java把内存划分成两种:一种是栈内存,另一种是堆内存 。
在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配 ,而实际的对象是在存储堆内存中 。即 :
int i = 5;//直接在栈中分配空间
Integer i = new Integr(5);//对象是在堆内存中,而i(引用变量)是在栈内存中 。在堆中分配的内存,由java虚拟机的自动垃圾回收器来管理.。
(4)当需要往ArrayList,HashMap中放东西时,像int,double这种基本类型是放不进去的,因为容器都是装object(对象类型)的,这就需要这些基本类型的封装类了,Java中每种基本类型都有相应的封装类。
2.适当练习valueOf()、xxxValue()、parseXxx()方法。
3.类型转换(以下为该周作业)。
(1)String转换为int
int i = Integer.parseOf(str);int i = Integer.valueOf(str).intValue();
找到第三种;
(2)String转换为Integer
两种构造方法的区别,Character只有一种(3)int转换为String
String s = s.valueOf(i);String s = Integer.toString(i);
String s = “”+i;
(4)int转换为Integer
(5)Integer转换为String
(6)Integer转换为int
(7)Integer转换为String
public class StudyDiary01
{
public static void main(String[] args){
//String --> int
String s = "111";
int i1 = Integer.parseInt(s); //直接使用静态方法,不会产生多余的对象,但会抛出异常
System.out.println("String --> int : i1=" + i1);
int i2 = Integer.valueOf(s).intValue(); //Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
System.out.println("String --> int : i2=" + i2);
int i3 = Integer.parseInt(s,10);//若int i = Integer.parseInt("123",16);即可以看成i = 1*16*16+2*16+3
System.out.println("String --> int : i3=" + i3 + '\n');
//int --> String
int j = 222;
String t1 = String.valueOf(j);//直接使用String类的静态方法,只产生一个对象
System.out.println("int --> String : t1=" + t1);
String t2 = Integer.toString(j); //直接使用静态方法,产生新的对象,会抛出异常
System.out.println("int --> String : t2=" + t2);
String t3 = "" + j; //会产生两个String对象
System.out.println("int --> String : t3=" + t3 + '\n');
//int --> Integer
int m = 333;
Integer n1 = m; //装箱
System.out.println("int --> Integer:n1=" + n1);
Integer n2 = new Integer(n1);
System.out.println("int --> Integer:n2=" + n2);
Integer n3 = Integer.valueOf(n2);
System.out.println("int --> Integer:n3=" + n3 + '\n');
//Integer --> int
Integer x=444;
int y1 = x; //拆箱
System.out.println("Integer --> int:y1=" + y1);
int y2 = x.intValue();
System.out.println("Integer --> int:y2=" + y2);
int y3 = Integer.valueOf(x);
System.out.println("Integer --> int:y3=" + y3 + '\n');
//String --> Integer
String r = "555";
Integer k1 = Integer.valueOf(r); //这种方法常用
System.out.println("String --> Integer:k1=" + k1);
Integer k2 = new Integer(r);
System.out.println("String --> Integer:k2=" + k2);
Integer k3 = Integer.parseInt(r);
System.out.println("String --> Integer:k3=" + k3);
Integer k4 = Integer.parseInt(r,10);
System.out.println("String --> Integer:k4=" + k4 + '\n');
//Integer --> String
Integer l = 666;
String z1 = String.valueOf(l);
System.out.println("Integer -->Stringf:z1=" + z1);
String z2 = l.toString();
System.out.println("Integer -->Stringf:z2=" + z2);
String z3 = "" + l;
System.out.println("Integer -->Stringf:z3=" + z3 + '\n');
}
}
4.用JAVA编程实现该功能:通过键盘输入,初始化两个数将这两个数值交换后,输出显示后的数据。
import java.util.Scanner;//引用Scanner包
public class StudyDiary02
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个值:");
int a = input.nextInt();
System.out.print("请输入第二个值:");
int b = input.nextInt();
System.out.println("交换之前的两个值为"+a+","+b);
int c;
c = a;
a = b;
b = c;
System.out.println("交换之后的两个值为"+a+","+b);
}
}
5.定义一个五位整数,求各个位上的数的和
import java.util.Scanner;//引用Scanner包
public class StudyDiary02
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个值:");
int a = input.nextInt();
System.out.print("请输入第二个值:");
int b = input.nextInt();
System.out.println("交换之前的两个值为"+a+","+b);
int c;
c = a;
a = b;
b = c;
System.out.println("交换之后的两个值为"+a+","+b);
}
}
import java.util.Scanner;//引用Scanner包
public class StudyDiary03
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个五位整数:");
int i = in.nextInt();
int sum = 0;
if (i < 10000 || i > 99999) {
System.out.println("输入有误!");
} else {
for (int j = 0; j < 5 ; j++ ) {
sum += i % 10;
i = i / 10;
}
System.out.println("该五位整数各位数上之和为sum=" + sum );
}
}
}
6.根据天数计算周数和剩余的天数。天数通过键盘输入。
import java.util.Scanner;//引用Scanner包
public class StudyDiary03
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个五位整数:");
int i = in.nextInt();
int sum = 0;
if (i < 10000 || i > 99999) {
System.out.println("输入有误!");
} else {
for (int j = 0; j < 5 ; j++ ) {
sum += i % 10;
i = i / 10;
}
System.out.println("该五位整数各位数上之和为sum=" + sum );
}
}
}
import java.util.Scanner;
public class StudyDiary04
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("请输入要转换的天数:");
int day = in.nextInt();
int week = day / 7;
day = day % 7;
System.out.println("为" + week + "周," + day + "天。");
}
}
7.学会使用API文档,看一看String类的所有方法。
import java.util.Scanner;
public class StudyDiary04
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("请输入要转换的天数:");
int day = in.nextInt();
int week = day / 7;
day = day % 7;
System.out.println("为" + week + "周," + day + "天。");
}
}
一、字符串的表示
字符串常量, 字符串常量使用双引号包含的一串字符,比如:"Hello world! "
Java编译器自动为每一个字符串常量生成一个String(首字母大写)类 的实例,因此可以用字符串常量直接初始化一个String对象,如:String s="Hello world! ";
二、构造函数
要创建类String的一个对象并进行初始化,需要调用类 String的构造方法。类String中提供了下面的一些构造方法:
(1) String( ),无参数的缺省的构造方法用来创建一个空串。
String s = new String( );
(2) String( String value):利用已经存在的字符串常量创建一个新的String对象,该对象的内容与给出的字符串常量一致。
String s=new String(“hello”);
(3) String( char value[ ] );通过给构造方法传递一个字符数组 可以创建一个非空串。
char chars[ ] = { ‘a’ , ‘b’ , ‘c’ };
String s = new String( chars ) ;
(4) String( char [ ] , int startIndex, int numChars ):这种方法用来创建一个非空串,并且指明所创建的字符串在字符数组中的起始地址以及所包含的字符个数。
char chars[] = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
String s = new String ( chars , 2 , 3 );该方法生成的串s为“cde”。(注意数组的下标从0开始)
三、求字符串长度
public int length( ) 此方法返回字符串的字符个数,如:
String s = "abc";
System.out.println( s.length() );
则将显示字符个数为3。
四、字符串连接
在Java中,运算符“+”可以用来实现字符串的连接,这点和C++类似,通过运算符重载“+”,几乎所有的面向对象编程语言都有这个字符串操作功能,如:
String s=“He is”+age+”years old.”
假设整数型变量age的值为15,那么,s的值为 “He is 15 yearsOld”。
五、字符截取
(1) public char charAt(int index ) 该方法返回字符串中index位置上的字符,其中index值的范围是0-length-1。如果要从字符串中提取一个以上的字符,则可以下面方法:
public void getchars(int srcbegin,int end,char buf[],int dstbegin)
其中,srcbegin为要提取的第一个字符在源串中的位置,end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的字符串,dstbegin为提取的字符串在目的串中的起始位置。
(2) public void getBytes (int srcbegin,int end,byte buf[],int dstbegin ) 类似于上一个方法,只是串中的字符均用8位表示,参数及用法同上。 在将字符串输出到一个不支持Unicode的环境时,getBytes极为有用,现今大多数基于Internet的文本交换以ASCII码为主,getBytes方法就可以派上用场,如做中文处理,在后面还专门介绍Java中的中文字
符处理的问题。
六、字符串比较
equals和equalsIgnoreCase
public boolean equals(object str)
public boolean equalsIgnoreCase(object str )
public int compareTo ( String str )
该方法按字典次序比较两个字符串的大小, 如果源串较小,则返回一个小于0的值,如相等则返回0,否则返回一个大于0的值。
例:
public class TestString {
public static void main(String[] args) {
String s1 = "Monday";
String s2 = new String("Monday");
s2 = s2.intern();
if (s1 == s2)
System.out.println("s1 == s2");
else
System.out.println("s1 != s2");
if (s1.equals(s2))
System.out.println("s1 equals s2");
else
System.out.println("s1 not equals s2");
}
}
七、字符串查找
int indexOf(String str)
int lastindexOf(String str)
返回子串str中第一个字符在字符串中出现的始末位置。
int indexOf(int ch, int fromIndex))
int lastIndexOf(int ch, int fromIndex))
返回字符ch在字符串中位置fromIndex以后出现的始末位置。
int indexOf(String str,int fromIndex)
int lastIndexOf(String str, int fromIndex)
返回子串str中的第一个字符在字符串中位置fromIndex以后出现的始末位置。
例:
function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}
八、字符串修改/替换
public String replace(char oldChar,char newChar)
将串中出现的特定字符用新的字符代替。
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
用来得到字符串中指定范围内的子串。
String trim()
该方法返回除去字符串左右的空格后的字符串
九、改变大小写
public String toLowerCase() 把串中的所有字符转换成小写
public String toUpperCase() 把串中的所有字符转换成大写
8.明确什么是引用类型,和C++的指针有什么异同,掌握java中数组的基本知识,例如(动态/静态)初始化,赋值,求大小,关于大小改变与C/C++中的区别。
一、字符串的表示
字符串常量, 字符串常量使用双引号包含的一串字符,比如:"Hello world! "
Java编译器自动为每一个字符串常量生成一个String(首字母大写)类 的实例,因此可以用字符串常量直接初始化一个String对象,如:String s="Hello world! ";
二、构造函数
要创建类String的一个对象并进行初始化,需要调用类 String的构造方法。类String中提供了下面的一些构造方法:
(1) String( ),无参数的缺省的构造方法用来创建一个空串。
String s = new String( );
(2) String( String value):利用已经存在的字符串常量创建一个新的String对象,该对象的内容与给出的字符串常量一致。
String s=new String(“hello”);
(3) String( char value[ ] );通过给构造方法传递一个字符数组 可以创建一个非空串。
char chars[ ] = { ‘a’ , ‘b’ , ‘c’ };
String s = new String( chars ) ;
(4) String( char [ ] , int startIndex, int numChars ):这种方法用来创建一个非空串,并且指明所创建的字符串在字符数组中的起始地址以及所包含的字符个数。
char chars[] = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
String s = new String ( chars , 2 , 3 );该方法生成的串s为“cde”。(注意数组的下标从0开始)
三、求字符串长度
public int length( ) 此方法返回字符串的字符个数,如:
String s = "abc";
System.out.println( s.length() );
则将显示字符个数为3。
四、字符串连接
在Java中,运算符“+”可以用来实现字符串的连接,这点和C++类似,通过运算符重载“+”,几乎所有的面向对象编程语言都有这个字符串操作功能,如:
String s=“He is”+age+”years old.”
假设整数型变量age的值为15,那么,s的值为 “He is 15 yearsOld”。
五、字符截取
(1) public char charAt(int index ) 该方法返回字符串中index位置上的字符,其中index值的范围是0-length-1。如果要从字符串中提取一个以上的字符,则可以下面方法:
public void getchars(int srcbegin,int end,char buf[],int dstbegin)
其中,srcbegin为要提取的第一个字符在源串中的位置,end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的字符串,dstbegin为提取的字符串在目的串中的起始位置。
(2) public void getBytes (int srcbegin,int end,byte buf[],int dstbegin ) 类似于上一个方法,只是串中的字符均用8位表示,参数及用法同上。 在将字符串输出到一个不支持Unicode的环境时,getBytes极为有用,现今大多数基于Internet的文本交换以ASCII码为主,getBytes方法就可以派上用场,如做中文处理,在后面还专门介绍Java中的中文字
符处理的问题。
六、字符串比较
equals和equalsIgnoreCase
public boolean equals(object str)
public boolean equalsIgnoreCase(object str )
public int compareTo ( String str )
该方法按字典次序比较两个字符串的大小, 如果源串较小,则返回一个小于0的值,如相等则返回0,否则返回一个大于0的值。
例:
public class TestString {
public static void main(String[] args) {
String s1 = "Monday";
String s2 = new String("Monday");
s2 = s2.intern();
if (s1 == s2)
System.out.println("s1 == s2");
else
System.out.println("s1 != s2");
if (s1.equals(s2))
System.out.println("s1 equals s2");
else
System.out.println("s1 not equals s2");
}
}
七、字符串查找
int indexOf(String str)
int lastindexOf(String str)
返回子串str中第一个字符在字符串中出现的始末位置。
int indexOf(int ch, int fromIndex))
int lastIndexOf(int ch, int fromIndex))
返回字符ch在字符串中位置fromIndex以后出现的始末位置。
int indexOf(String str,int fromIndex)
int lastIndexOf(String str, int fromIndex)
返回子串str中的第一个字符在字符串中位置fromIndex以后出现的始末位置。
例:
function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}
八、字符串修改/替换
public String replace(char oldChar,char newChar)
将串中出现的特定字符用新的字符代替。
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
用来得到字符串中指定范围内的子串。
String trim()
该方法返回除去字符串左右的空格后的字符串
九、改变大小写
public String toLowerCase() 把串中的所有字符转换成小写
public String toUpperCase() 把串中的所有字符转换成大写
-
引用是变量的别名,指针是变量的地址;
-
引用不能独立存在,定义时必须初始化,指针则不然;
-
对某变量的引用的操作就是对该变量本身的操作,能引起变量永久性改变,而对某变量指针的操作并不影响变量值,只有通过指针对其目标的操作才能永久性改变指针所指的目标值。
-
引用在寿命期内不能再定义为其他变量的引用,指针却可以指向其他变量。
-
不能引用一个数组,但有数组的指针。
-
没有引用的引用,却有指针的指针。