java基础学习

Java数据类型

基本类型(primitive type)

  • 数值类型:
    {整数类型
    byte 占1个字节符范围:-128 - 127
    short 占2个字节符范围:-32768 - 32767
    int 占4个字节符范围:-2147483648 - 2147483647
    long 占8个字节符范围:-9223372036854774808 - 9223372036854774807
    浮点类型
    float 占4个字节符
    double 占8个字节符
    字符类型
    char占2个字节符
    }
  • boolean类型
    占1位其值只有true和false两个
public class PrimitiveType {
    public static void main(String[] args) {
        //整数类型
        byte a = 127;
        short b = 32767;
        int c = 2147483647;
        long d = 9223372036854774807L;//long类型需要加L来区分
        //小数类型
        float e = 1.2F;//float类型需要用F来区分
        double f = 1.3;
        //布尔值:是非
        boolean g = true;
        //boolean g = false;
        //整数类型
        char h = '中';
    }
}

引用数据类型(reference type)

  • 接口
  • 数组

基本数据类型扩展及面试题

/**
 * 基本数据类型扩展
 */
public class PrimitiveType1 {
public static void main(String[] args) {
    //整数扩展:进制 二进制0b 十进制 八进制0 十六进制0x
    int i = 1;
    int i2 = 010; //八进制
    int i3 = 0x10; //十六进制
    System.out.println(i);//输出1
    System.out.println(i2);//输出8
    System.out.println(i3);//输出16
    //浮点数扩展
    float f = 0.1F;//0.1
    double d = 1.0 / 10;//0.1
    System.out.println(f == d);//false
    float a1 = 232323232132312F;
    float a2 = a1 + 1;
    System.out.println(a1 == a2);// true
    /**有限 离散 舍入误差 大约 接近但不等于
       最好完全规避使用浮点数进行比较
       解决方法BigDecimal,数学工具类
     **/
    //字符扩展
    char c1 = 'a';
    char c2 = '中';
    System.out.println((int) c1);//强制转换97
    System.out.println((int) c2);//强制转换20013
    //所有字符本质都是数字
    //布尔值扩展
    boolean flag = true;
    if (flag){
    }//==if(flag == true){}
}
}

类型转换

public class Demo4 {
    /**类型转换
     强制转换 高>低  (类型)变量名
     自动转换 低>高
    */
    public static void main(String[] args) {
        int a = 128;
        byte a1 = (byte) a;
        System.out.println(a);//输出128
        System.out.println(a1);//内存溢出 输出-128
        /*
        注意点:
        1.不能对boolean值转换
        2.不能把对象类型转换成不相干的类型
        3.在把低容量转换成高容量时要强制转换
        4.转换时可能出现内存溢出,或者小数时精度问题
         */
        System.out.println((int) 12.8);
        System.out.println((int) -345.4F);
        char c = 'a';
        int c1 = c + 1;
        System.out.println(c1);//输出98
        System.out.println((char) c1);//输出b
    }
}

变量

/**
 * 变量:可以变动的值
 * 类变量
 * 局部变量
 * 实例变量
 */
public class Demo5 {
    //类变量
    static double a = 10;
    /*实例变量
    如果不初始化,这个类型的默认值 0 ,0.0 ,
    boolean默认false
    引用类型默认null
    */
    String name;
    int age;

    public static void main(String[] args) {
        //局部变量
        int b = 1;
        System.out.println(b);
        //实例变量
        Demo5 demo5 = new Demo5();
        System.out.println(demo5.age);
        System.out.println(demo5.name);
        //类变量
        System.out.println(a);
    }
}

常量

在这里插入代码public class Demo6 {
    //常量(Constant)初始化(initialize)后不可以被改变的值!不会变动的值
    static final double MAX_VALUE = 10;
    public static void main(String[] args) {
        System.out.println(MAX_VALUE);
    }
}

自增

public class Demo7 {
    //++自增
    //--自减
    public static void main(String[] args) {
        int a = 1;
        int c = a ++ ;//先给c赋值,再自增
        //a = a + 1;
        System.out.println(a);//输出2
        //a = 1 + a;
        int d = ++ a ;//先自增,再给c赋值
        System.out.println(a);//输出3
        System.out.println(c);//输出1
        System.out.println(d);//输出3
    }
}

三元运算符

public class Demo8 {
    //三元运算:   x ? y : z
    //如果x为true 则输出 y 否则输出 z
    public static void main(String[] args) {
        double score = 70;
        System.out.println(score >= 60 ? "及格" : "不及格");
    }
}

Scanner

.next()方法

import java.util.Scanner;

/**
 * Scanner.next()
 * 1.读取到有效的字符才可以结束
 * 2.输入有效字符之前遇到的空白,next()方法自动去掉
 * 3.只有输入有效字符后才可以将空白作为分隔符或者结束符
 * 4.next()不能得到带有空格的字符串
 */
public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请打印一句话");
        if (scanner.hasNext()){
            String str = scanner.next();
            System.out.println(str);
        }
        scanner.close();//结束
    }
}

.nextLine()方法

import java.util.Scanner;
/**
 * scanner.nextLine()
 * 1.以Enter为结束符,.nextLine()方法返回回车之前所有的字符串
 * 2.可以获得空白
 */
public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请打印一句话");
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println(str);
        }
        scanner.close();
    }
}

.hasNextInt()方法和.hasNextFloat()方法

import java.util.Scanner;

/**
 1. .hasNextInt()整数
 2. .hasNextFloat()

小数
 */
public class Demo03 {

    public static void main(String[] args) {
        int a = 0;
        float d = 0.0F;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入正数");
        if (scanner.hasNextInt()){
            a = scanner.nextInt();
            System.out.println("数字是" + a);
        }else{
            System.out.println("你输入的不是正数");
    }
        System.out.println("请输入小数");
            if (scanner.hasNextFloat()){
            d = scanner.nextFloat();
                System.out.println("你输入的是" + d);
        }else{
            System.out.println("你输入的不是小数");
        }

        scanner.close();
    }

}

.hasNextDouble()方法

import java.util.Scanner;
/**
 * .hasNextDouble()
 */

public class Demo04 {
    //我们可以输入多个数字但是要求数字的总和 和 平均值,通过输入非数字为结束符
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double sum = 0.0;
        int m = 0;
        System.out.println("请输入数字");
        while (scanner.hasNextDouble()){
            double v = scanner.nextDouble();
            m++;
            sum = v + sum;
        }
        System.out.println("平均值:" + (sum / m) );
        System.out.println("总和:" + sum);
        scanner.close();
    }
}

九九乘法表


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

//打印99乘法表
public class Demo {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + (  i * j) + "\t" );
            }
            System.out.println();
        }
    }
}

打印三角形

package com.loop;
//打印一个五行的三角形
public class Demo001 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5 ; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print("*");
            }
            for (int j = 1; j <= i ; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int j = 5; j >= i ; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

冒泡排序

package com.lin.www.array;

import java.util.Arrays;

//冒泡排序
/**
1.比较数组中,两个相邻的元素,如果第一个比第二个数大,我们就交换他们的位置
2.每次比较都会产生一个最大值和一个最小值
3.下一轮则可以少一次排序
4.依次循环直至结束
*/
public class ArrayDemo01 {
    public static void main(String[] args) {
        int[] a = {1,214,24124,124155,414142};
        System.out.println(Arrays.toString(sort(a)));
    }
    public static int[] sort(int[] arrays){
        int math = 0;
        for (int i = 0; i < arrays.length-1 ; i++) {
            for (int j = 0; j < arrays.length-1-i; j++) {
                if (arrays[j + 1] < arrays[j]){
                    arrays[j] = math;
                    arrays[j + 1] = math;
                    arrays[j] = arrays[j + 1];
                }
            }
        }
        return arrays;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值