Java学习(二)

Java中的基本类型数据(char、bool、byte、short、int、long、float、double)长度:

 1         System.out.println(Byte.SIZE/8);
 2         System.out.println(Short.SIZE/8);
 3         System.out.println(Integer.SIZE/8);
 4         System.out.println(Float.SIZE/8);
 5         System.out.println(Long.SIZE/8);    
 6         System.out.println(Double.SIZE/8);
 7         i='';
 8         System.out.println(i);
 9         c=22919;
10         System.out.println(c);

显示结果为:1、2、4、4、8、8、22909、妇。

char为两个字节,long为8个字节,这个与C++中的一个字节和四个字节不同。char为2个字节的好处是可以除了表示ASCII码外还可以表示汉字。

 在Java中char为无符号类型。int、float、double没有无符号类型,若要表示无符号类型,则需要使用更长字节的类型来模拟。如int用long来模拟无符号类型。

1 /**
2          * 
3          * Java中模拟无符号类型
4          * 
5          */
6         l=-7;//用于将有符号的整型的-7转换为无符号的数值
7         i=(int)(l & 0x0000FFFF);
8         System.out.println(i);

在Java中存在引用类型(主要包括类、数组、结构体)数据。其中的数据内存放在堆中,在栈中只存放堆所在的地址。这与基本类型不同。基本类型的数据内存均在栈中。因此在赋值方面,两者存在本质的区别:

 1 /**
 2          * 基本类型数据与引用类型数据在赋值方面的不同
 3          */
 4         
 5         int i2=0;
 6         i2=i;
 7         System.out.println(i+" "+i2);
 8         Test test1=new Test(3);
 9         Test test2=test1;
10         test1.run();
11         test2.set(7);
12         test1.run();
13 
14 
15 
16 class Test
17 {
18     private int n=0;
19     public Test(int temp){n = temp;}
20     public void set(int temp){n=temp;}
21     public void run(){System.out.println(n);}
22 }

结果显示,65529 65529 3 7

test2可以修改test1的数据。因为二者共用同一块堆,在栈中,二者存放的地址相同。

 在Java中有以下运算符:&位与、|位或、^位异或、&&逻辑与、||逻辑或、~取反、!非、>>左移、<< 右移。

另外Java还增加了无符号右移符号>>>,即右移时右侧补零。

 1 /**
 2          * 无符号类型char与有符号类型int的位移操作
 3          */
 4         c=0xa0ff;
 5         print("c:    ",c);
 6         print("c<<2: ",c<<2);
 7         print("c>>3: ",c>>3);
 8         print("c>>>3:",c>>>3);
 9         
10         i=0xa0ffffff;
11         print("i:    ",i);
12         print("i<<2: ",i<<2);
13         print("i>>3: ",i>>3);
14         print("i>>>3:",i>>>3);
15         
16 
17 void print(String prefix,int n)
18     {
19         String s=Integer.toBinaryString(n);
20         while(s.length()<4)//长度少于4时补0
21             s="0"+s;
22         System.out.println(prefix+s);
23         System.out.println(n);
24     }

显示结果为:

 1 c:    1010000011111111
 2 41215
 3 c<<2: 101000001111111100
 4 164860
 5 c>>3: 1010000011111
 6 5151
 7 c>>>3:1010000011111
 8 5151
 9 i:    10100000111111111111111111111111
10 -1593835521
11 i<<2: 10000011111111111111111111111100
12 -2080374788
13 i>>3: 11110100000111111111111111111111
14 -199229441
15 i>>>3:10100000111111111111111111111
16 337641471

说明无符号移位对int类型有效,对char无效。另外,只有int和long具有toBinaryString()函数。

在Java中没有全局变量。因此static修饰符充当全局变量的角色。

this为类中指向对象及方法中的一种指针。在类中,static修饰的对象供所有类的实例使用,即只开辟一个内存空间。

this的用途有以下两点:1.可以指向类中对象与方法,避免与方法中变量重名;2.在构造方法中指向其他构造方法(前提是该指向必须在第一句)

 1 package helloWorld;
 2 
 3 public class Class {
 4     private int n;
 5     public Class()
 6     {
 7         this(1);//相当于public Class(1){this.n=n;}
 8     }
 9     public Class(int n)
10     {
11         this.n=n;
12     }
13     
14     void print()
15     {
16         System.out.println(n);
17     }
18 }

结果为1

通俗来讲,static就是没有this 的方法。

switch的case可以接常字符、常整型、常字符串。在Java中,表示常量用final而不是用const,final为最基本类型,不可继承。

Java中用scanner无法读入一个字符,因此可以通过读取字符串的方式获取字符串第一个字符,间接的读取一个字符。

 1 final char c1='A';
 2         final char c2='B';
 3         final char c3='C';
 4         
 5         final int i1=1;
 6         final int i2=2;
 7         
 8         final String s1="str1";
 9         final String s2="str2";
10         final String s3="str3";
11         String temp="";
12         
13         Scanner scanner=new Scanner(System.in);
14         temp=scanner.nextLine();
15         char c=temp.charAt(0);//读取字符
16         int i=scanner.nextInt();
17         scanner.nextLine();//读取'
'字符
18         String str=scanner.nextLine();
19         scanner.close();
20         
21             
22         switch(c)
23         {
24         case c1:System.out.println(c1);
25         break;
26         case c2:System.out.println(c2);
27         break;
28         case c3:System.out.println(c3);
29         break;
30         default:System.out.println("other character!");
31         }
32         
33         switch(i)
34         {
35         case i1:System.out.println(i1);
36         break;
37         case i2:System.out.println(i2);
38         break;
39         case 3:System.out.println(i);
40         break;
41         default:System.out.println("other integer!");
42         }
43         
44         switch(str)
45         {
46         case s1:System.out.println(s1);
47         break;
48         case s2:System.out.println(s2);
49         break;
50         case s3:System.out.println(s3);
51         break;
52         default:System.out.println("other string!");
53         }
54     }

在Java中去除了goto语句,取而代之的是continue和break。其中continue具有类似于goto的功能。goto语句在C语言中是一种跳转语句,类似于汇编语言中Jump,可以从一个程序跳转到另一个程序。但是可读性差,在C语言中一直存在争议。continue单独使用表示执行写一条循环指令,continue order1表示跳转到order1指向的循环。break单独使用表示跳出本循环,break order2表示跳出

order2指向的循环。对于其他的一些非循环指令,continue与break(除switch……case……外)均不可用。

 1 public void run() {
 2         /*
 3          * 求1-100所有的质数 每10个数换一次行 采用continue和break语句
 4          */
 5         int n = 1;
 6         int i = 2;
 7         int j = 0;
 8         boolean flag = false;
 9         /*
10          * continue的用法类似于C语言的goto语句
11          */
12         out1: for (i = 3; i < 100; i++) {
13             for (j = 2; j < i; j++)
14                 if (i % j == 0)
15                     continue out1;// 跳转到out1
16             System.out.print(i + " ");
17             n++;
18             if (n % 10 == 0)
19                 System.out.println();
20         }
21         System.out.println();
22         n = 1;
23 
24         /*
25          * 方法二,直接使用break
26          */
27         out3: for (i = 3; i < 100; i++) {
28             flag = true;
29             out2: for (j = 2; j < i; j++)
30                 if (i % j == 0) {
31                     flag = false;
32                     // break out3;直接跳出循环,得到值为3
33                     break out2;
34                 }
35 
36             if (flag) {
37                 n++;
38                 System.out.print(i + " ");
39                 if (n % 10 == 0)
40                     System.out.println();
41                 flag = false;
42             }
43         }
44     }
原文地址:https://www.cnblogs.com/2Bthebest1/p/8401293.html