Java入门知识点

 

Java源代码的流程

  Java程序由.java文件生成,通过JVM进行编译得到字节文件.class

  1. class HelloWorld {
  2. public static void main(String[] args){
  3. System.out.println("Hello World!");
  4. }
  5. }

关于class有如下几点规则:

  1. 文件的名字必须和class的名字一致(public级别的class名字)。
  2. 文件必须只包含一个public访问基本的class(可以包含多个非public级别的class)。

命名规则:

  1. 包(其实就是文件夹,用于解决相同类名问题):包名要求全部小写,一般是公司的域名倒着写 com.heima.包的作用
  2. 类或者接口:如果是一个单词,要求首字母大写,如果是多个单词要求每个单词首字母大写(驼峰命名)
  3. 方法和变量: 如果是一个单词,每个字母都小写,如果是多个单词,从第二个单词开始首字母大写
  4. 常量: 如果是一个单词,所有字母大写,如果是多个单词也是所有字母大写,但是用_分开 MAX MAX_VALUE

Java语言优势:

  完全面向对象,安全可靠,跨平台性

(补充)有关于dos命令行方式:

  dir  md  rd  cd  cd..  cd/  del  exit

标识符

  由英文字母,数字,_ ,$组成,其中数字不能开头

数据类型(11)

 8种原子类型(基本数据类型)

  1. 整数类型:byte(1)、short(2)、int(4)和long(8)。
  2. 小数类型:float(4)和double(8)。
  3. 字符类型:char(2)。
  4. 布尔类型:bool。

 3种引用数据类型

  interface、class和array

 小数类型的常量默认是double类型,声明float类型的常量需要使用F作为后缀。

运算符

  1. 算术运算符:+、-、*、/ 和 %,两个整数相除,结果还是整数。
  2. 赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、~=、^=、<<=、>>= 、 >>>=、++ 和 --。
  3. 比较运算符:==、!=、<、<=、> 和 >=。
  4. 逻辑运算符:&&、|| 和 !。
  5. 位运算符:&、|、~、^、<<、>> 和 >>>。

注意:

  1. 字符串数据和任何数据使用+都是相连接,最终都会变成字符串。//System.out.println(“5+5=”+5+5);//5+5=55
  2. 出现负数取余只看左边。//System.out.println(-1%5);
  3. s+=5;//只一次赋值运算,进行自动转换
  4. &和&&的区别 [&:无论左边是true是false,右边都运算 &&:当左边为false时,右边不运算。同理,|和||的区别一致]

键盘录入

  1. import java.util.Scanner;
  2. class Demo_Scanner {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("请输入一个整数:");
  6. int x=sc.nextInt();
  7. System.out.println(x);
  8. }
  9. }

控制结构

  1. 条件:if-else if-else、switch-case-default和三元运算符(?:)。
  2. 循环:while、do-while、for和foreach。
  3. Labeled block。

注意:

  1. if else结构简写格式:变量=(条件表达式)?表达式1:表达式2;
  2. switch语句中被选择类型的值只有四种:byte short int char;
  3. switch语句里case和default定义位置随机,执行顺序从case开始。
  4. 语句嵌套形式:循环嵌套(大圈套小圈)

  //尖朝上,可改变条件,让条件随着外循环变化
  //尖朝下,可初始化值,让初始化随着外循环变化

  5. break与continue的区别

  break:应用范围:选择结构和循环体 跳出所在当前循环
  continue:只能用于循环结构 结束本次循环,继续下一次循环

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. task: {
  5. int age = 25;
  6.  
  7. System.out.println("start");
  8.  
  9. if (age < 30) {
  10. break task;
  11. }
  12.  
  13. System.out.println("end");
  14. }
  15. }
  16. }

字符串

  String是拥有“值语义”的引用类型,字符串常量实现了“享元模式”,equals会按照内容进行比较,==按照地址比较。

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. String x = "小咕噜";
  5. String y = new String("小咕噜");
  6.  
  7. System.out.println(x.equals(y)); // true
  8. System.out.println(x == y); // false
  9. }
  10.  
  11. }

 为了高效的修改字符串Java引入了StringBuffer

  1. {
  2. StringBuffer sb =
  3. new StringBuffer()
  4. .append("小")
  5. .append("咕")
  6. .append("噜");
  7.  
  8. System.out.println(sb.toString());
  9. }

数组

 声明语法

  DataType[] name 或 DataType name[]。

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. {
  5. String[] strs = { "小", "咕", "噜" };
  6.  
  7. for (String item : strs) {
  8. System.out.print(item);
  9. }
  10. }
  11. }
  12.  
  13. }

 多维数组

  只有不等长多维数组DataType[][],没有DataType[xxx, xxx]。

方法

  Java中所有的赋值和方法调用都是“按值“处理的,引用类型的值是对象的地址,原始类型的值是其自身。

  Java支持变长方法参数。

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. print("小咕噜", "大咕噜");
  5. print(new String[] { "小咕噜", "大咕噜" });
  6. }
  7.  
  8. private static void print(String... args) {
  9. for (String item : args) {
  10. System.out.println(item);
  11. }
  12. }
  13. }

 ① 变量在内存中的存储方式:  

  1. 成员变量作用于整个类中,存储于堆内存中,因为对象的存在,才在内存中存在,必有初始化值,可参与运算
  2. 局部变量作用于函数中或语句中,存在于栈内存中

  this的应用:

  1. 当定义类中功能时,该函数内部要调用该函数的对象 但凡本类功能内部使用了本类对象,都用this表示。
  2. 用于区分局部变量和成员变量重名的情况代表本类的对象,代表它所在函数所属对象的引用。
  3. 只能定义在构造函数的第一行。

 ③ 构造代码块: 给所有对象进行统一初始化,优先于构造函数执行

注意:(内存机构:分区)
  1.栈内存:用于存储局部变量,当数据使用完,所占空间会自动释放
  2.堆内存:①.数组和对象,通过New建立的实例都存放在堆内存中
       ②.每一个实体都有内存地址值
       ③.实体中的变量都有默认初始化值
       ④.实体不再被使用,会在不确定的时间内被垃圾回收器回收
  3.方法区,本地方法区,寄存器

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. Point point = new Point(100);
  5.  
  6. System.out.print(point);
  7. }
  8. }
  9.  
  10. class Point {
  11. private int x = 0;
  12. private int y = 0;
  13.  
  14. public Point(int x, int y) {
  15. this.x = x;
  16. this.y = y;
  17. }
  18.  
  19. public Point(int x) {
  20. this(x, x);
  21. }
  22.  
  23. public String toString() {
  24. return "(x:" + this.x + ",y:" + this.y + ")";
  25. }
  26. }

静态成员

  Java中类似静态构造方法的结构,称之为:静态初始化代码块,与之对应的是实例初始化代码块,见下例:

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println(Point.getValue());
  5. System.out.println(new Point());
  6. }
  7. }
  8.  
  9. class Point {
  10. private static int value = 0;
  11.  
  12. public static int getValue() {
  13. return value;
  14. }
  15.  
  16. static {
  17. value++;
  18. }
  19.  
  20. static {
  21. value++;
  22. }
  23.  
  24. private int x = 0;
  25. private int y = 0;
  26.  
  27. {
  28. this.x = 10;
  29. }
  30.  
  31. {
  32. this.y = 10;
  33. }
  34.  
  35. public String toString() {
  36. return "(x:" + this.x + ",y:" + this.y + ")";
  37. }
  38. }

注意:

  1. 静态方法只能访问静态成员 非静态方法既可以静态也可以访问非静态
  2. 静态方法中不可以定义this,super等关键字 因为静态优先于对象存在
  3. 主函数是静态的
  4. 类变量随着类的加载而存在于方法区中 实例变量随着对象的建立而存在于堆内存中

继承

  继承使用 extends,抽象类和抽象方法使用abstract声明,向下转型使用 (ChildType)instance,判断是否是某个类型使用 instanceof,见下例:

  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. printAnimal(new Animal());
  5. printAnimal(new Dog());
  6. }
  7.  
  8. private static void printAnimal(Animal animal) {
  9. if(animal instanceof Dog){
  10. System.out.println("I am a " + (Dog) animal);
  11. }
  12. else
  13. {
  14. System.out.println("I am an " + animal);
  15. }
  16. }
  17. }
  18.  
  19. class Animal {
  20. public String toString() {
  21. return "Animal";
  22. }
  23. }
  24.  
  25. class Dog extends Animal {
  26. public String toString() {
  27. return "Dog";
  28. }
  29. }

重写

 Java中的重写规则比较灵活,具体如下:

  1. 除了 private 修饰之外的所有实例方法都可以重写,不需要显式的声明。
  2. 重写的方法为了显式的表达重写这一概念,使用 @Override进行注解。
  3. 重写的方法可以修改访问修饰符和返回类型,只要和父类的方法兼容(访问级别更高,返回类型更具体)。
  4. 可以使用final将某个方法标记为不可重写。
  5. 在构造方法中使用 super(xxx, xxx)调用父类构造方法,在常规实例方法中使用 super.method(xxx, xxx)调用父类方法。
  6. Java不支持覆盖(new)。
  1. public class Program {
  2.  
  3. public static void main(String[] args) {
  4. Animal animal = new Animal();
  5. Animal dog = new Dog();
  6.  
  7. animal.say();
  8. dog.say();
  9.  
  10. animal.eat(animal);
  11. dog.eat(dog);
  12.  
  13. System.out.println(animal.info());
  14. System.out.println(dog.info());
  15. }
  16. }
  17.  
  18. class Animal {
  19. private String name = "Animal";
  20.  
  21. protected void say() {
  22. System.out.println("Animal" + " " + this.name);
  23. }
  24.  
  25. public void eat(Animal food) {
  26. System.out.println("Animal eat " + food);
  27. }
  28.  
  29. public Object info() {
  30. return "Animal";
  31. }
  32.  
  33. @Override
  34. public String toString() {
  35. return "Animal";
  36. }
  37. }
  38.  
  39. class Dog extends Animal {
  40. private String name = "Dog";
  41.  
  42. @Override
  43. public final void say() {
  44. System.out.println("Dog" + " " + this.name);
  45. }
  46.  
  47. @Override
  48. public final void eat(Animal food) {
  49. super.eat(food);
  50.  
  51. System.out.println("Dog eated");
  52. }
  53.  
  54. @Override
  55. public final String info() {
  56. return "Dog";
  57. }
  58.  
  59. @Override
  60. public final String toString() {
  61. return "Dog";
  62. }
  63. }

Java支持三种导入语法:

  1. 导入类型:import xxx.xxx.xxxClass。
  2. 导入包:import xxx.xxx.xxx.*。
  3. 导入静态成员:import static xxx.xxx.*。
  1. import static util.Helper.*;
  2.  
  3. public class Program {
  4.  
  5. public static void main(String[] args) {
  6. puts("小咕噜");
  7. }
  8. }

访问级别

  Java支持四种访问级别:public、private、protected 和 default(默认),类型和接口只能使用public 和 default,成员和嵌套类型可以使用所有,下面简单的解释一下 protected 和 default。

  • protected 修饰过的成员只能被自己、子类和同一个包里的(不包括子包)其他类型访问。
  • default 修改过的类型或成员只能被自己和同一个包里的(不包括子包)其他类型访问。

嵌套类

Java支持如下几种嵌套类:

  1. nested class,定义在类型内部的类型。
    1. static nested class,使用 static 声明的 nested class,static nested class 可以访问所有外部类的静态成员。
    2. inner class,没有使用 static 声明的 nested class,inner class 可以访问所有外部类的实例成员,inner class 不能定义静态成员。
  1. public class Program {
  2.  
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. OuterClass outer = new OuterClass();
  8. OuterClass.InnerClass inner = outer.new InnerClass();
  9. OuterClass.InnerClass.InnerInnerClass innerInner = inner.new InnerInnerClass();
  10. outer.show();
  11. inner.show();
  12. innerInner.show();
  13.  
  14. OuterClass.StaticNestedClass staticNested=new OuterClass.StaticNestedClass();
  15. OuterClass.StaticNestedClass.StaticNestedNestedClass staticNestedNested=new OuterClass.StaticNestedClass.StaticNestedNestedClass();
  16.  
  17. staticNested.show();
  18. staticNestedNested.show();
  19. }
  20. }
  21.  
  22. class OuterClass {
  23. int x = 1;
  24. static int i = 1;
  25.  
  26. void show() {
  27. System.out.println(x);
  28. System.out.println(i);
  29. }
  30.  
  31. class InnerClass {
  32. int y = 2;
  33.  
  34. void show() {
  35. System.out.println(x);
  36. System.out.println(y);
  37. }
  38.  
  39. class InnerInnerClass {
  40. int z = 3;
  41.  
  42. void show() {
  43. System.out.println(OuterClass.this.x);
  44. System.out.println(y);
  45. System.out.println(z);
  46. }
  47. }
  48. }
  49.  
  50. static class StaticNestedClass {
  51. static int j = 2;
  52.  
  53. void show() {
  54. System.out.println(i);
  55. System.out.println(j);
  56. }
  57.  
  58. static class StaticNestedNestedClass {
  59. static int k = 3;
  60.  
  61. void show() {
  62. System.out.println(i);
  63. System.out.println(j);
  64. System.out.println(k);
  65. }
  66. }
  67. }
  68. }

特殊的inner class:local class

  1. public class LocalClassExample {
  2.  
  3. static String staticValue = "static value";
  4. String instanceValue = "instance value";
  5.  
  6. public void test() {
  7.  
  8. final String finalLocalValue = "final local value";
  9.  
  10. class LocalClass {
  11. void test() {
  12. System.out.println(staticValue);
  13. System.out.println(instanceValue);
  14. System.out.println(finalLocalValue);
  15. }
  16. }
  17.  
  18. LocalClass local = new LocalClass();
  19. local.test();
  20. }
  21. }

除了inner class的规则之外,local class可以访问局部final变量,在Java8中有更多的改进。

注意:

1.被final修饰的类不可以被继承,为了避免被继承,被子类复写功能

2.被final修饰的方法不可以被复写

3.被final修饰的变量是一个常量,只能赋值一次i,既可以修饰成员变量,也可以修饰局部变量 作宏定义使用

4.内部类定义在类中的局部位置上时,只能访问该局部被final修饰的局部变量

特殊的local class:anonymous class

  1. public class Program {
  2.  
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. execute(new Action() {
  8. @Override
  9. public void execute() {
  10. System.out.println("执行业务逻辑");
  11. }
  12. });
  13. }
  14.  
  15. static void execute(Action action) {
  16. System.out.println("事物开始");
  17. action.execute();
  18. System.out.println("事物结束");
  19. }
  20. }
  21.  
  22. interface Action {
  23. void execute();
  24. }

常量

  1. public final class Program {
  2. static final String STATIC_CONSTANTS = "STATIC_CONSTANTS";
  3. final String INSTANCE_CONSTANTS = "INSTANCE_CONSTANTS";
  4.  
  5. public static void main(String[] args) {
  6. final String LOCAL_CONSTANTS = "LOCAL_CONSTANTS";
  7.  
  8. System.out.println(STATIC_CONSTANTS);
  9. System.out.println(new Program().INSTANCE_CONSTANTS);
  10. System.out.println(LOCAL_CONSTANTS);
  11. new Program().test("PARAMETER_CONSTANTS");
  12. }
  13.  
  14. public final void test(final String msg) {
  15. System.out.println(msg);
  16. }
  17. }

有一点需要注意的是:只有一种情况Java的常量是编译时常量(编译器会帮你替换),其它情况都是运行时常量,这种情况是:静态类型常量且常量的值可以编译时确定。

接口

Java的接口可以包含方法签名、常量和嵌套类,见下例:

  1. public final class Program {
  2. public static void main(String[] args) {
  3. Playable.EMPTY.play();
  4.  
  5. new Dog().play();
  6. }
  7. }
  8.  
  9. interface Playable {
  10. Playable EMPTY = new EmptyPlayable();
  11.  
  12. void play();
  13.  
  14. class EmptyPlayable implements Playable {
  15.  
  16. @Override
  17. public void play() {
  18. System.out.println("无所事事");
  19. }
  20.  
  21. }
  22. }
  23.  
  24. class Dog implements Playable {
  25.  
  26. @Override
  27. public void play() {
  28. System.out.println("啃骨头");
  29. }
  30.  
  31. }

异常

Java中的异常分为checked和unchecked,checked异常必须声明在方法中或被捕获,这点我觉得比较好,必定:异常也是API的一部分,见下例:

  1. public final class Program {
  2. public static void main(String[] args) {
  3. try {
  4. test();
  5. } catch (Exception e) {
  6. System.out.println(e.getMessage());
  7. }
  8. }
  9.  
  10. public static void test() throws Exception {
  11. throw new Exception("I am wrong!");
  12. }
  13. }

所有继承Exception的异常(除了RuntimeException和它的后代之外)都是checked异常。

 注意:

  throws:使用在函数上,后面跟的是异常类,可以跟多个,用逗号隔开
  throw:使用在函数内,跟的是异常对象

Java入门知识点的更多相关文章

  1. java入门知识点结构

    第一部分    计算机程序和面向对象编程 编程语言种类: 机器语言:2进制(0和1) 汇编语言:英文字符缩写和助记符 高级语言: 面向过程:面向过程是从微观上/细节上处理具体事务. C语言 面向对象: ...

  2. Java入门知识点:

    1.跨平台性主要原理是:在需要运行的java应用程序的操作系统上安装了一个对应操作系统对应版本的JVM(Java Virtual Machine)java虚拟机即可,由JVM来负责Java程序的在该系 ...

  3. Java入门学习知识点汇总

    Java入门重要知识点在这里总结一下,以方便日后复习,这部分内容主要有:变量和常量,常用的运算符,流程控制语句,数组,方法这些内容 一.变量和常量 1.Java关键字 先贴张图: 所有关键字区分大小写 ...

  4. 新手学Java,有哪些入门知识点?

    很多小伙伴们在刚接触Java的时候,会有些迷茫,不知道该从哪里入手,不管是做前端还是后端,程序员都会用到JAVA,那该掌握哪些必要的基础知识呢.今天就跟大家分享新手学Java,有哪些入门知识点? 下面 ...

  5. Java入门基础知识点总结(详细篇)

    Java入门基础知识点总结(详细篇)~~~~~目录 1.1 图解 1.1.1 Java基础知识点 1.1.2 Java基础语法的相关内容 1.2 关键字 1.3 标识符 1.3.1 标识符概念 1.3 ...

  6. 第1章Java入门体验

    第1章Java入门体验 1.java简介和平台应用 Java是sun公司开发出来,现在属于ORACLE公司java分为几个部分:首先是最基础的Java SE部分,这部分是Java的基础知识,主要包括: ...

  7. Java入门-浅析Java学习从入门到精通【转】

    一. JDK (Java Development Kit)  JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库 ...

  8. Java入门篇(六)——类和对象

    写到这里终于写到了入门篇的最后一个知识点了.类和对象是Java中经常被提到的两个词汇,实际上可以将类看作对象的载体,它定义了对象所具有的功能.Java是面向对象的语言,因此掌握类与对象是学习Java语 ...

  9. Java 面试知识点解析(四)——版本特性篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

随机推荐

  1. SSM商城项目(十)

    1.   学习计划 1.使用freemarker实现网页静态化 a)Freemarker的使用方法 b)Freemarker模板的语法 c)Freemarker整合springmvc 2.Active ...

  2. mysql 库 表 和 时间查询

    -- 查询 worker 库中 表 和 视图 select table_name from information_schema.tables where table_schema='worker' ...

  3. Python人工智能之路 - 第三篇 : PyAudio 实现录音 自动化交互实现问答

    Python 很强大其原因就是因为它庞大的三方库 , 资源是非常的丰富 , 当然也不会缺少关于音频的库 关于音频, PyAudio 这个库, 可以实现开启麦克风录音, 可以播放音频文件等等,此刻我们不 ...

  4. 改变this的指向问题;

    用call()和apply()改变this的指向,那什么时候用this呢?(构造函数),那为什么要用构造函数呢?(为了生成对象). 1.解决函数内this指向的问题 (1)var that/_this ...

  5. 粒子动画——Pygame

    你是否也想做出下图这么漂亮的动态效果?想的话就跟着我一起做吧=.= 工具: Python--Pygame 仔细观察上图,你能发现哪些机制呢?再在下面对比一下是否跟你想的一样. 运行机制: 1.随机方向 ...

  6. 14. pt-kill

    pt-kill h=192.168.100.101,P=3306,u=admin,p=admin \--match-user "user01" \--match-host &quo ...

  7. Python:每日一题004

    题目: 输入某年某月某日,判断这一天是这一年的第几天? 程序分析: 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天 个人的思路及 ...

  8. Scrum冲刺阶段1

    各个成员在 Alpha 阶段认领的任务 人员 任务 何承华 美化设计 部分后端设计 陈宇 后端设计 丁培辉 美化设计 部分后端设计 温志铭 前端设计 杨宇潇 服务器搭建 张主强 前端设计 明日各个成员 ...

  9. 微信公众号自定义菜单中添加emoji表情

    做微信公众号开发,可能会遇到如何加入emoji表情的问题.今天在“海南旅游小管家”公众号的菜单中加入了emoji表情,特此记录备忘. 1.登录微信公众号,在左侧找到[开发者工具]菜单,点击进入,找到[ ...

  10. Linux 第五天

    网络命令 1)write 给在线用户发信息(需按Crtl+D保存结束,w命令可看在线用户) 语法:write 用户名 2)wall 发广播信息 英文原意:write all 语法:wall 信息 3) ...