ITA-JAVA

  • Java语言是解释型的
  • Java 是大小写敏感的
  • JVM包含在JRE中,而JDK具有自己的JRE。
  • 许多人错误地认为true,false和null是关键字,但实际上不是。它们只是文字。
  • 变量命名方式:小写驼峰大写,整个单词的首字母应小写,但随后的首字母应大写。
  • 常量命名方式:字母都应大写,两个相邻单词之间用下划线(_)分隔。
  • 类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写。
  • 方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写。
  • 主方法入口:所有的 Java 程序由 public static void main(String[] args) 方法开始执行。
  • Java仅允许多级继承,而不是多继承。子类只能继承父类的非私有属性和行为
  • 子类可以(隐式或显式)调用而不是继承其父类的构造函数
  • 一个类只能继承一个抽象类,但可以实现多个接口。
  • 类变量:类变量也声明在类中,方法体之外,但必须声明为 static 类型。
  • 一个源文件中只能有一个 public 类,可以有多个非 public 类,源文件的名称应该和 public 类的类名保持一致
  • 抽象类可以有静态方法

Java修饰符

Java可以使用修饰符来修饰类中方法和属性。主要有两类修饰符:

  • 访问控制修饰符 : default, public , protected, private
  1. default (即默认,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。

  2. private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)

  3. public : 对所有类可见。使用对象:类、接口、变量、方法

  4. protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)

修饰符当前类同一包内子孙类(同一包)子孙类(不同包)其他包
publicYYYYY
protectedYYYY/N(说明N
defaultYYYNN
privateYNNNN
  • 父类中声明为 public 的方法在子类中也必须为 public。

  • 父类中声明为 protected 的方法在子类中要么声明为 protected,要么声明为 public,不能声明为 private。

  • 父类中声明为 private 的方法,不能够被继承。

  • 类和接口不能声明为 private,声明为私有访问类型的变量只能通过类中公共的 getter 方法被外部类访问。

  • 非访问控制修饰符 : final, abstract, static, synchronized
  1. static 修饰符,用来修饰类方法和类变量。
  2. final 修饰符,用来修饰类、方法和变量,final 修饰的类不能够被继承,修饰的方法不能被继承类重新定义,修饰的变量为常量,是不可修改的。
  3. abstract 修饰符,用来创建抽象类和抽象方法。
  4. synchronized 和 volatile 修饰符,主要用于线程的编程。
  • 静态方法不能使用类的非静态变量。
  • 一个类不能同时被 abstract 和 final 修饰。
  • 抽象方法是一种没有任何实现的方法,该方法的的具体实现由子类提供。
  • 抽象方法不能被声明成 final 和 static。
  • 任何继承抽象类的子类必须实现父类的所有抽象方法,除非该子类也是抽象类。
  • 如果一个类包含若干个抽象方法,那么该类必须声明为抽象类。抽象类可以不包含抽象方法。

Java 变量

  • 局部变量
  1. 局部变量声明在方法、构造方法或者语句块中;
  2. 局部变量在方法、构造方法、或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁;
  3. 访问修饰符不能用于局部变量;
  4. 局部变量只在声明它的方法、构造方法或者语句块中可见;
  5. 局部变量是在栈上分配的。
  6. 局部变量没有默认值,所以局部变量被声明后,必须经过初始化,才可以使用。
  • 类变量(静态变量)
  1. 类变量也称为静态变量,在类中以 static 关键字声明,但必须在方法之外。
  2. 无论一个类创建了多少个对象,类只拥有类变量的一份拷贝。
  3. 静态变量除了被声明为常量外很少使用。常量是指声明为public/private,final和static类型的变量。常量初始化后不可改变。
  4. 静态变量储存在静态存储区。经常被声明为常量,很少单独使用static声明变量。
  5. 静态变量在第一次被访问时创建,在程序结束时销毁。
  6. 与实例变量具有相似的可见性。但为了对类的使用者可见,大多数静态变量声明为public类型。
  7. 默认值和实例变量相似。数值型变量默认值是0,布尔型默认值是false,引用类型默认值是null。变量的值可以在声明的时候指定,也可以在构造方法中指定。此外,静态变量还可以在静态语句块中初始化。
  8. 静态变量可以通过:ClassName.VariableName的方式访问。
  9. 类变量被声明为public static final类型时,类变量名称一般建议使用大写字母。如果静态变量不是public和final类型,其命名方式与实例变量以及局部变量的命名方式一致。
  • 成员变量(非静态变量)
  1. 实例变量声明在一个类中,但在方法、构造方法和语句块之外;
  2. 当一个对象被实例化之后,每个实例变量的值就跟着确定;
  3. 实例变量在对象创建的时候创建,在对象被销毁的时候销毁;
  4. 实例变量的值应该至少被一个方法、构造方法或者语句块引用,使得外部能够通过这些方式获取实例变量信息;
  5. 实例变量可以声明在使用前或者使用后;
  6. 访问修饰符可以修饰实例变量;
  7. 实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符可以使实例变量对子类可见;
  8. 实例变量具有默认值。数值型变量的默认值是0,布尔型变量的默认值是false,引用类型变量的默认值是null。变量的值可以在声明时指定,也可以在构造方法中指定;
  9. 实例变量可以直接通过变量名访问。但在静态方法以及其他类中,就应该使用完全限定名:ObejectReference.VariableName。

Java基本数据类型

Type nameType meaningbyte digitsdefault valuemaximum valueminimum value
bytebyte type80127(2^7-1)-128(-2^7)
shortshort int type16032767(2^15-1)-32768(2^15)
intint type3202147483647(2^31-1)-2147483648(2^31)
longlong int type6409223372036854775807(2^63-1)-9223372036854775807(2^63)
floatsingle-precision floating-point type320.0fnullnull
doubledouble-precision floating-point type640.0dnullnull
booleanboolean type1falsetrue(1)false(0)
charcharacter type16\u0000\uffff(65,53)\u0000(0)

自动类型转换

低  ------------------------------------>  高
byte,short,char—> int —> long—> float —> double 
  1.  不能对boolean类型进行类型转换。
  2. 不能把对象类型转换成不相关类的对象。
  3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。
  4. 转换过程中可能导致溢出或损失精度
  5. 浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入
  6. 定义 float 类型时必须在数字后面跟上 F 或者 f。

位运算符

下表列出了位运算符的基本运算,假设整数变量 A 的值为 60 和变量 B 的值为 13:

操作符描述例子
如果相对应位都是1,则结果为1,否则为0(A&B),得到12,即0000 1100
|如果相对应位都是 0,则结果为 0,否则为 1(A | B)得到61,即 0011 1101
^如果相对应位值相同,则结果为0,否则为1(A ^ B)得到49,即 0011 0001
按位取反运算符翻转操作数的每一位,即0变成1,1变成0。(〜A)得到-61,即1100 0011
<< 按位左移运算符。左操作数按位左移右操作数指定的位数。A << 2得到240,即 1111 0000
>> 按位右移运算符。左操作数按位右移右操作数指定的位数。A >> 2得到15即 1111
>>> 按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。A>>>2得到15即0000 1111

短路逻辑运算符

当使用与逻辑运算符时,在两个操作数都为true时,结果才为true,但是当得到第一个操作为false时,其结果就必定是false,这时候就不会再判断第二个操作了。


Java数组

声明数组变量

double[] myList;         // 首选的方法
double myList[];         //  效果相同,但不是首选方法

创建数组

int[] a = {1,2,3,4,5};
int[] b=new int[5];

 


 switch case 

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。


Java equals 和 hashCode

  1.  equals 方法默认比较的是两个对象的引用是否指向同一个内存地址。
  2.  hashCode 这是一个 native 本地方法,其实默认的 hashCode 方法返回的就是对象对应的内存地址。 
  3. 调用 equals 返回 true 的两个对象必须具有相等的哈希码。
  4. 如果两个对象的 hashCode 返回值相同,调用它们 equals 方法不一返回 true 。

面试题目: hashCode 方法返回的是对象的内存地址么?
答: Object 基类的 hashCode 方法默认返回对象的内存地址,但是在一些场景下我们需要覆写 hashCode 函数,比如需要使用 Map 来存放对象的时候,覆写后 hashCode 就不是对象的内存地址了。

面试题: hashCode 方法的作用和意义
答: 在 Java 中 hashCode 的存在主要是用于提高容器查找和存储的快捷性,如 HashSet, Hashtable,HashMap 等,hashCode是用来在散列存储结构中确定对象的存储地址的,


equals 和 ==

  • 对于基本数据类型 == 操作符判断的是左右两边变量的值
  • 对于引用数据类型 == 操作符判断就是等号两边的指向的对象的内存地址是否相同。也就是说通过 == 判断的两个引用数据类型变量,如果相同,则他们指向的肯定是同一个对象。
  • Object 基类的 equals 默认比较两个对象的内存地址,在构建的对象没有重写 equals 方法的时候,与 == 操作符比较的结果相同。
  • equals 用于比较引用数据类型是否相等。在满足equals 判断规则的前体系,两个对象只要规定的属性相同我们就认为两个对象是相同的。

What is the size of the variables of int class?

  • 8 bit
  • 16 bit
  • 32 bit
  • 64 bit

Basic data types are saved in stack?(基本数据类型是否是保存在栈中?)

  • true
  • false

stack:栈;heap:堆

  1. 所有的基本数据类型全部是存储在栈里面,速度快。
  2. 一个对象,它的实体是存储在堆里面的,而它的引用是存储在栈里面。

基本数据类型分配给堆栈,而引用类型分配给堆


Which of below descriptions is wrong?

  • Instance variable is member variable of class.(实例变量是类的成员变量。)
  • Instance variable is declared by the keyword of static.(实例变量由static关键字声明)
  • Method variable is created during method execution.(方法变量在方法执行期间创建)
  • Method variable must be initialized before using.(使用前必须初始化方法变量)

What are the variables defined in a certain method called?(在某个方法中定义的变量称为什么?)

  • Instance variable(实例变量)
  • Local variable(局部变量)
  • Class variable(类变量)
  • Hidden variable(隐藏变量)

Which of below are Java variable names which make sense?

  • aaa
  • 22aa
  • Name
  • name
  • userEmail

Which of below are method names which make sense?

  • aaa
  • name
  • getName
  • setName

int number = 10;
boolean b = ((number = 50) > 5);
After executing above codes, what will be the value of number?
  • 10
  • 50
  • true
  • false

int i = 13;
i  = i << 2;
After executing above codes, what will be the value of i?
  • 13
  • 26
  • 52
  • 104

<< :左移两位 2^2=4,13*4=52;

如果换成>>,则13➗4

对于int类型,移动的位数是32的余数


long  i = 13;
i = i << 65;
After executing above codes, what will be the value of i?
  • 13
  • 26
  • 52
  • 104

对于double类型,移动的位数是64的余数,所以在这相当移了一位


int i = 10;
int j = 10;
boolean b = (i > 25 && ((j = 20) > 15));

System.out.println ("b = " + b);
System.out.println ("i = " + i);
System.out.println ("j = " + j);
After executing above codes, what will be the output value?
  • b = true i = 10 j = 20
  • b = true i = 10 j = 10
  • b = false i = 10 j = 20
  • b = false i = 10 j = 10

int i = 15; i = i++;

 After executing above codes, what will be the value of i?

  • 14
  • 15
  • 16
  • 17

int i = 15; int j = i++; After executing above codes, what will be the value of i?

  • 14
  • 15
  • 16
  • 17

What's the final output value for below codes?

int sum = 0;
for (int i = 0; i < 10; i++) {
     sum = sum++;
}
System.out.println(sum);

答案:0


public static void main(String args[]) {
    char ch = '8';
    int r = 10;
    switch (ch + 1){
        case '7' : r = r + 3;break;
        case '8' : r = r + 5;break;
        case '9' : r = r + 6;break;
        default: r = r + 8;
    }
    System.out.println(r);
} ```
What is the output for above codes?
  • 13
  • 15
  • 16
  • 18

If both a and b are integer variables and have been assigned values properly, which of below is a correct switch statement?

  • switch(a+b); { ...... }
  • switch( a+b*3.0 ) { ...... }
  • switch a { ...... }
  • switch ( a%b ) { ...... }

switch参数只支持整数类型


Which of below is the variable type for switch statement?

  • byte
  • String
  • char
  • Enum

Which of below is an Error, which cannot be processed by the programme?

  • NullPointerException
  • OutOfMemoryErrorArray (内存溢出)
  • IndexOutOfBoundsException
  • FileNotFoundException

Which of below is the correct description of the custom exceptions?(以下哪项是自定义异常的正确描述?)

  • Custom exceptions must inherit Exception(自定义异常必须继承Exception)
  • Custom exceptions can inherit Error(自定义异常可以继承Error)
  • Custom exceptions can locate the exception more accurately with detailed information (自定义异常可以通过详细信息更准确地定位异常)
  • There is no sense to use custome exceptions as the programme has a rich variety of exception types.(使用自定义异常没有意义,因为该程序具有多种异常类型。)

自定义异常可以继承Exception,也可以继承自己定义的异常类。


What's the keyword used to throw an exception in java?(Java中用于引发异常的关键字是什么?)

  • try
  • catch
  • throw
  • finally

In programming, what should we do with statements which have been defined to throw an exception in certain cases?(在编程中,我们应该如何处理已定义为在某些情况下引发异常的语句?)

  • Use try/catch statements to hanle exceptions, or use throw statements to throw them.(使用try / catch语句处理异常,或使用throw语句引发异常。)
  • If there is anything wrong with the programme, try/catch statements should be used to hanle exceptions.(如果程序有任何问题,应使用try / catch语句处理异常。)
  • Just ignore them.(只是忽略它们。)
  • We can only use try/catch statements to deal with it.(我们只能使用try / catch语句来处理它。)

In the statement block of try-catch-finally, which of below can be used alone with finally?

  • catch
  • try
  • throws
  • throw

String s = null; s.concat("abc") What exception will we get for running above codes?

  • ArithmeticException
  • NullPointerException
  • IOException
  • ClassNotFoundException

Which of below is right on object-oriented?

  • In programming we can use class to simulate physical entity in the real world.
  • For each physical entity we should create a class.
  • The behavior and attributes of an object have been packaged in class, which need to be accessed by calling the method of the class. But first of all you should know how the class is realized inside.
  • Some of the entities in real world cannot be described by class.

class Student {
    void m1() {
        this.m2();
    }

    void m2() {
        this = null;
        System.out.println(this);

    }
}
 public class Main {
    public static void main(String[] args) {
        Student s = new Student();

        s.m1();
    }
} 
What output will you get for above codes?
  • null
  • Student
  • cannot assign a value to final variable this
  • this

Which description is correct on abstract class?(哪个描述对抽象类是正确的?)

  • Abstract class doesn't have to include abstract methods.(抽象类不必包含抽象方法。)
  • Abstract class shall never include constructor methods.(抽象类不得包含构造函数方法。)
  • The abstract methods in abstract class should have subclass implementation.(抽象类中的抽象方法应具有子类实现。)
  • Abstract class shall never include static methods.(抽象类不得包含静态方法。)

抽象类可以不包含抽象方法,
包含抽象方法的类一定是抽象类。


Which description is correct on interface?(接口上的哪个描述正确?)

  • Interface can only use single inheritance.(接口只能使用单继承。)
  • The methods in interface are always modified by public.(接口中的方法始终由公共修改。)
  • Interfaces only include constants and method definition.(接口仅包含常量和方法定义)
  • In interface we can declare static methods.(在接口中,我们可以声明静态方法。)

interface Choices {
        int YES = 1;
        int NO = 2;
} 
public class Main {
   public static void main(String[] args) {
      System.out.println("Choices.YES = " + Choices.YES);
      System.out.println("Choices.NO = " + Choices.NO);
      Choices.NO = 9;//Error:cannot assign a value to final variable NO
      System.out.println("Choices.NO = " + Choices.NO);

   }
} 
What are the output values for the above three statements?
  • Choices.YES = 1 Choices.NO = 2 Choices.NO = 9
  • Choices.YES = 1 Choices.NO = 2 Choices.NO = 2
  • Choices.YES = 1 Choices.NO = 2 Choices.NO = null 
  • Error:cannot assign a value to final variable NO

Which is wrong on abstract class and interface?(抽象类和接口哪一个错?)

  • In Java, the abstract class indicate a relationship of inheritance. One class can only apply one inheritance, but can implement multiple interfaces.(在Java中,抽象类指示继承关系。一类只能应用一种继承,但是可以实现多个接口)
  • The abstract class doesn't have to include abstract methods.(抽象类不必包含抽象方法)
  • It is a compile-time error if an interface has the same simple name as any of its enclosing classes or interfaces(如果接口与其任何封闭的类或接口具有相同的简单名称,则是编译时错误)
  • The variable defined in interface is of the public static final type by default, which needs to be given a initial value. Therefore in implementation class it cannot be redefined and its value cannot be changed.(默认情况下,在interface中定义的变量为public static final类型,需要给它一个初始值。因此,在实现类中,不能重新定义它,并且不能更改其值。)
  • All the methods in an interface should be modified by the keyword of public.(接口中的所有方法都应使用public关键字进行修改。)

接口方法用public修饰不是必须的。


Which of below is accessc control modifiers?

  • static
  • public
  • private
  • final

What’s the output of the following codes?

    public static void main(String[] args) {
        int sum = 10;
        add(sum);

        System.out.println(sum);
    }

    private static void add(int sum) {
        sum = sum + 5;
    }

答案:10


System.out.printf("'%1.4s'", "Java"); Which is the output of the above codes?

  • \"Java\"
  • Java
  • \'Java\'
  • \"Java\'

String str=String.format("%,d",12345) Which is the output of the above codes?

  • 12345
  • 12,345
  • 123,45
  • 1234,5 

String str=String.format("%+,.1f",1234.56); Which is the output of the above codes?1,234.5

  • +12,34.5
  • +123,4.5
  • +1,234.6

String str=String.format("%.3f",123.4567); Which is the output of the above codes?

  • 123.4567
  • 123.457
  • 1234.567
  • 1234.56

Which is the output of the above codes? System.out.println(String.format("%06d", 123));

  •  123
  • 123
  • 123000
  • 000123

String str=String.format("Hi,%s:%s.%s", "java","js","html"); System.out.println(str); Which is the output of the above codes?

  • Hi,java:js.html
  • java:js.html
  • Hi,java,js.html
  • java.js.htm
String str = 1 + 2 + "abc";
What is the str value after execution of the above codes? 
  • 12abc
  • 3abc
  • 12"abc"
  • 3"ab

String str = "abc"+ 1 + 2 ;
What is the str value after execution of the above codes?
  • abc12
  • abc3
  • "abc"12
  • "abc"3

Which description about String is incorrect?(关于字符串的哪些描述不正确?)

  • String cannot be changed;(字符串不能更改;)
  • We can create String with the operator new;(我们可以用new运算符创建String;)
  • String belongs to basic data type;(字符串属于基本数据类型;)
  • We can judge whether two String values are equivalent to each other through equals.(我们可以通过相等来判断两个String值是否相等。)

String是一个类,不属于基本数据类型


public static void main(String[] args) {
    String str1 = "ab";
    String str2 = "abc";
    String str3 = str1 + "c";
    System.out.println(str2 == str3);
    System.out.println(str2.equals(str3));
}

 What’s the output sequence after execution of the above codes?

  • true true
  • true false
  • false true
  • false false

str2经过了运算所以new了新对象,内存地址和str3不一样


Where is an array stored in Java?

  • Stack
  • Queue
  • Heap
  • Chain table
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值