• Java入门学习笔记


    Hello.java

    1 public class Hello {
    2 public static void main(String[] args) {
    3 System.out.println("Hello, world!");
    4 }
    5 }
    文件名必须和程序的类名完全一致。扩展名是.java,编译文件为.class  
    一个project对应一个目录,源码存放在src目录, 编译输出存放在bin目录, bin目录在eclipse中自动隐藏。
    计算机最小存储单元是字节,一个字节8个二进制数00000000~11111111(1B=8b).
    变量分为两种:基本类型和引用类型。
    基本类型(持有某个数值):
    • 整数类型:byte(1B),short(2B),int(4B),long(8B)
    • 浮点类型:float(4B),double(8B)
    • 字符类型:char(2B)
    • 布尔类型:boolean
     1         final double PI = 3.1415;
     2         byte b = 127; // -128 ~ +127
     3         short s = 32767; // -32768 ~ +32767
     4         int i = 2147483647;
     5         int i2 = -2147483648;
     6         int i3 = 2_000_000_000; // 加下划线更容易识别
     7         int i4 = 0xff0000; // 表示16进制的数
     8         int i5 = 0b100000;// 表示二进制数
     9         long l = 90000000000L; // 结尾加L
    10         float f = 3.14e4f;
    11         double f2 = 3.14e4;
    12 
    13         System.out.println(Integer.toHexString(11)); // 输出16进制表示的整形
    14         System.out.println(Integer.toBinaryString(10)); // 输出二进制表示的整形
    • 与运算特点:把某些位保留下来,其余位全部置0
    • 或运算特点:把某些位保留下来,其余位全部置1
    • ^异或运算:相同为0不同为1
    1         double a=0.0/0; // NaN(Not a number)
    2         double a1=1.0/0; // Infinity
    3         double a2=-1.0/0; // -Infinity

     java使用Unicode表示字符

    字符串类型是引用类型(变量指向某个对象,而不是持有某个对象)

    1         char c1='A';
    2         char c2='中';
    3         int n1=c1; // 65
    4         int n2=c2; // 20013
    5         char c3='u0041'; //  u加4位的16进制数也可表示字符
    6         String s="中文123"; // 五个字符
    7         String s1="12
    "; // 三个字符
    8         String s2=null; // 输出null
    9         String s3=""; // 和null不一样

     数组是引用类型的变量

    1         int[] ns1 = new int[5];
    2         int[] ns2 = new int[] { 1, 2, 3, 4, 5 };
    3         int[] ns3 = { 1, 2, 3, 4, 5 };
    4         int[] ns4=ns3;
    5         ns3[3]=999; // ns[4]=999,它们指向同一个数组
    6         System.out.printf("%1$s %3$s %2$s %1$s", "A", "B", "C"); // A C B A $可以调整参数位置
    1         int[] ns = {1, 2, 3};
    2         System.out.println(Arrays.toString(ns));
    3         int[][] ns1= {{1}, {1, 2}, {1, 2, 3}};
    4         System.out.println(Arrays.deepToString(ns1));

    命令行参数是String[], 由用户输入并传递给main方法,如何解析命令行参数由程序自己实现。

    this作用:

    1. 表示类中的属性
    2. 可以使用this调用本类的构造方法
    3. this表示当前对象,如下
     1 public boolean compare(Person per) {
     2         Person per1 = this; //表示当前调用方法的对象,为per1
     3         Person per2 = per;
     4         if(per1 == per2)
     5             return true;
     6         if(per1.name.equals(per2.name) && per1.age==per2.age) {
     7             return true;
     8         }else {
     9             return false;
    10         }
    11     }

    Java中主要存在4块内存空间:

    1. 栈内存空间:保存所有对象的名称(准确说保存了引用的堆内存地址)
    2. 堆内存空间:保存了每个对象的具体属性内容
    3. 全局数据区:保存static类型的属性
    4. 全局代码区:保存所有的方法定义

    非static声明的方法可以去调用static声明的属性或方法, 但是static声明的方法是不能调用非static类型声明的属性或方法

    抽象类就是比普通类多了一个抽象方法, 除了不能直接进行对象的实例化操作之外并没有任何的不同。

    接口:一种特殊的类,明确由全局常量和公共的抽象方法组成,可简写。

    1 public interface A {
    2     public static final String NAME = "XiaoMing";// 全局变量,等价于Stirng NAME = "XiaoMing"
    3     public abstract void print(); // 定义抽象方法 , 等价于void print();
    4     public abstract String getInfo(); // 定义抽象方法,等价于String getInfo();
    5     // 接口方法默认public, 不是default。
    6 }

     

    若一个子类既要实现接口又要继承抽象类,如下

    1 class 子类 extends 抽象类 implements 接口A, 接口B,...{
    2 }

    允许一个抽象类实现多个接口, 但一个接口不允许继承抽象类, 但是允许一个接口继承多个接口,如下

    1 interface 子接口 extends 父接口A,父接口B,...{
    2 }

     除了在抽象类中定义接口及在接口中定义抽象类外, 对于抽象类来说也可以在内部定义多个抽象类, 而一个接口也可以在内部定义多个接口。

    对象在向下转型先前用instanceof判断是否是某的类的实例, true后在转型。

    object可接收一切引用类型,包括数组和接口类型。

    1 int x = 30;
    2 Integer i = new Integer(x)
    3 = Integer i = 30;//自动装箱
    4 
    5 int temp = i.intValue;
    6 = int temp=i;//自动拆箱
    1 // Integer类(字符串转int型)
    2 public static int parseInt(String s) throws NumberFormatException
    3 // Float类(字符串转float型)
    4 public static float parseFloat(String s) throws NumberFormatException(异常属于RuntimeException可以不使用Try...catch处理,有异常交给JVM处理)
    5 以上两种操作,字符串必须由数字组成。(Integer.parseInt,Float.parseFloat这样调用)

     

    如果一个程序导入多个包中有同名类, 调用时“包.类名称”。

    Thread类中提供了public Thread(Runnable target)和public Thread(Runnable target, String name)两个构造方法,可以接受Runnable接口的子类实例对象,可以以此启动多线程,

    Thread和Runnable的子类都同时实现了Runnable的接口,之后将Runnable的子类放到Thread类之中,类似代理设计。

    Runnable接口可以资源共享:

     1 public class Mythread extends Thread{
     2 
     3     private int ticket = 5;
     4     public void run() {
     5         for(int i=0;i<100;i++) {
     6             if(ticket>0) {
     7                 System.out.println("买票:ticket="+ticket--);
     8             }
     9         }
    10     }
    11 }
    12 public class Main {
    13     
    14     public static void main(String args[]) {
    15         Mythread my1 = new Mythread();
    16         Mythread my2 = new Mythread();
    17         Mythread my3 = new Mythread();
    18         my1.start();
    19         my2.start();
    20         my3.start();
    21     }
    22 }

     终断线程:

     1 public class Mythread implements Runnable{
     2 
     3     public void run() {
     4         System.out.println("进入run方法");
     5         try {
     6             Thread.sleep(5000);
     7             System.out.println("休眠结束");
     8         }catch(Exception e) {
     9             System.out.println("休眠被终止");
    10             return;
    11         }
    12         System.out.println("休眠正常结束");
    13     }
    14 }
    15 public class Main {
    16 
    17     public static void main(String[] args) {
    18 
    19         Thread t = new Thread(new Mythread());
    20         t.start();
    21         try {
    22             Thread.sleep(2000);
    23         }catch(Exception e) {};
    24         t.interrupt();
    25     }
    26 }

    线程礼让:

     1 public class Mythread implements Runnable{
     2     @Override
     3     public void run() {
     4 
     5         for(int i=1; i<=5; i++) {
     6             System.out.println(Thread.currentThread().getName()+"运行"+i);
     7             if(i==3)
     8             {
     9                 System.out.println("线程礼让");
    10                 Thread.currentThread().yield();
    11             }
    12         }
    13     }
    14 public class Main {
    15 
    16     public static void main(String[] args) {
    17 
    18         Mythread my = new Mythread();
    19         Thread t1 = new Thread(my, "线程A");
    20         Thread t2 = new Thread(my, "线程B");
    21         t1.start();
    22         t2.start();
    23     }
    24 
    25 }

     

    线程操作实例:生产者与消费者(生产者一直生产,消费者不断取走)

    存在两个问题:

    1. 在线程中生产者没有添加完信息,消费者就取走,信息联系错乱。( 解决:同步方法synchronized )
    2. 线程中生产者放了多次消费者不取或消费者取了多次已取过的信息。 ( 解决: 加flag标记,同时利用object类中的等待wait()与唤醒notify() )

    代码:

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4 
     5         Info info = new Info();
     6         Producer p = new Producer(info);
     7         Consumer c = new Consumer(info);
     8         new Thread(p).start();
     9         new Thread(c).start();
    10     }
    11 }
    12 public class Info {
    13 
    14     private boolean flag = false;
    15     private String name = "LiXinghua";
    16     private String concent = "Java";
    17 
    18     public synchronized void set(String name, String concent) {
    19         if (!flag) {
    20             try {
    21                 super.wait();
    22             } catch (InterruptedException e) {
    23                 e.printStackTrace();
    24             }
    25         }
    26         this.name = name;
    27         try {
    28             Thread.sleep(100);
    29         } catch (InterruptedException e) {
    30             e.printStackTrace();
    31         }
    32         this.concent = concent;
    33         flag = false;
    34         super.notify();
    35     }
    36 
    37     public synchronized void get() {
    38         if (flag) {
    39             try {
    40                 super.wait();
    41             } catch (InterruptedException e) {
    42                 e.printStackTrace();
    43             }
    44         }
    45         try {
    46             Thread.sleep(100);
    47         } catch (InterruptedException e) {
    48             e.printStackTrace();
    49         }
    50         System.out.println(this.name + "   " + this.concent);
    51         flag=true;
    52         super.notify();
    53                 
    54     }
    55 }
    56 public class Producer implements Runnable {
    57 
    58     boolean flag = false;
    59     private Info info = null;
    60 
    61     public Producer(Info info) {
    62         this.info = info;
    63     }
    64 
    65     @Override
    66     public void run() {
    67         for (int i = 0; i < 10; i++) {
    68 
    69             if (flag) {
    70                 this.info.set("LiXinghua", "Java");
    71                 System.out.println("修了几次");
    72                 flag = false;
    73             } else {
    74                 this.info.set("mldn", "JavaMldn");
    75                 System.out.println("修了几次");
    76                 flag = true;
    77             }
    78         }
    79     }
    80 }
    81 public class Consumer implements Runnable{
    82 
    83     private Info info = null;
    84     public Consumer(Info info) {
    85         this.info = info;
    86     }
    87     @Override
    88     public void run() {
    89 
    90         for(int i=0; i<10; i++) {
    91             this.info.get();
    92         }
    93         
    94     }

    停止进程常用方法:在Mythread中调用stop()将flag设置为false这样run()方法就停止运行

    代码:

     1 public class Mythread implements Runnable {
     2 
     3     private boolean flag = true;
     4 
     5     @Override
     6     public void run() {
     7 
     8         int i = 0;
     9         while (this.flag) {
    10             while (true) {
    11                 System.out.println(i++);
    12             }
    13         }
    14     }
    15     public void stop() {
    16         this.flag = false;
    17     }
    18 public class Main {
    19 
    20     public static void main(String[] args) {
    21 
    22         Mythread my = new Mythread();
    23         Thread t = new Thread(my, "线程啊");
    24         t.start();
    25         my.stop();
    26     }
    27 
    28 }

     

    返回值类型 方法名称(Object...args)表示方法可以接收任意多个参数

     1 public class Main {
     2 
     3     public static void main(String args[]) {
     4         
     5         String str[] = fun("lasd", "asd", "111");
     6         for(String t:str) {
     7             System.out.print(t+"、");
     8         }
     9     }
    10     public static <T> T[] fun(T... args) {
    11         return args;
    12     }
    13 }

    finalize()可以让一个对象被回收前执行操作:

    1     @Override
    2     public String toString() {
    3         return "姓名:" + this.name + ", " + "年龄:" + this.age;
    4     }
    5     public void finalize() throws Throwable{
    6         System.out.println(this);
    7     }

     格式化日期操作:

    public SimpleDateFormat(String pattern) 构造类型 ,通过一个指定的模板构造对象

    public Date parse(String sourse) throws ParseException 普通类型,将一个包含日期的字符串变为Date类型

    public final String format(Date date) 普通类型, 将一个Date类型按照指定格式变为String类型

     1 import java.util.Date;
     2 import java.text.DateFormat;
     3 import java.text.SimpleDateFormat;
     4 public class Main {
     5 
     6     public static void main(String args[]) {
     7 
     8         Date d = null;
     9         String strDate = "2018-8-26 17:11:30.214";
    10         String pat1 = "yyyy-MM-dd hh:mm:ss.SSS";
    11         String pat2 = "yyyy年MM月dd日, hh时mm分ss秒SSS毫秒啦!";
    12         
    13         SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
    14         SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);
    15         try {
    16             d = sdf1.parse(strDate);
    17         } catch (Exception e) {
    18             e.printStackTrace();
    19         }
    20         System.out.println(sdf2.format(d));
    21     }
    22 }

     

     

     

     

     

     

     

                                                 

  • 相关阅读:
    C++实现数字媒体三维图像渲染
    C++实现数字媒体三维图像变换
    C++实现数字媒体二维图像变换
    C++实现glut绘制点、直线、多边形、圆
    语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient)
    Java中的BigDecimal类精度问题
    spring 手册
    bootstrap 参考文档
    springBoot入门文章
    JNDI
  • 原文地址:https://www.cnblogs.com/dongdong25800/p/9491695.html
Copyright © 2020-2023  润新知