27天学会java

此方法只针对个人

public class Dome {
    public static void main(String[] args) {
       //这个用于复制的
        System.out.println();
    }
}

基础语法

变量,数据类型

什么是变量?
变量是一个变化的东西,可以改变值的东西
例如:int a = 1 这个a就是变量他的值就是1
变量可以干嘛?
变量可以存储数据,上例a变量里面的数据就是1
还可以:int b = 2 b变量里的数据就是2
怎么使用变量?
我们可以定义一个变量
例如:int a;int a,b,c...
或者:int a = 1;
变量使用的细节
属性的变量是有默认值的
方法里面的变量是要赋初始值的

public class Dome {
    public static void main(String[] args) {
       int a;
       System.out.println(a);//编译报错,是不会通过的没有赋初始值
    }
}

什么是数据类型?
在java中有两大类数据类型:
1,基本数据类型(这有四大种)
1.1 整型
byte,shot,int,long
1	 2	  4   8
1.2 浮点型
float,double
浮点型就是小数,flaot:4字节,double:8字节
1.3 字符型
char(单个字符用单引号括起来)例如:
char c = 'a';
字符的本身就是数字,他们用一些表来作为参照物例如:
ASCLL(一个字节),UNCOLDD(两个字节),UTF-8(变化的)
1.4 布尔型
boolean(true/false) 1byte
2,引用数据类型(这里只要在堆里面new出来的就是)
数据类型是干嘛的?
数据类型可以存储不同的东西,即万事万物!,存取简单
怎么使用数据类型?
数据类型 变量名 = 变量值;例如:
int a = 1; float f = 1.23f; char c = 'a'; boolean b = true;
数据类型的细节
整型默认类型是int
浮点数的默认类型是float(如果你需要定义一个float类型的数据后面需要添加一个f)
数字类型之间是可以相互转换的,小的转换大的是可以的但是大的转换成为小的是不可以的。例如:
int --》 double 这个可以
double --》 int 这个不可以
字符型的本身就是个数字即也可以发生转换

public class Dome {
    public static void main(String[] args) {
        int a = 1;
        float f = 1.0f;//小数的默认类型是double
        char c = 'a';
        boolean d = true;
        a = f;//错误的float是无法自动转换成为int的,
        f = a;//可以自动转换的
    }
}

运算符

算术运算符

public class Dome {
    public static void main(String[] args) {
       //+ - * / %
        /*
        %有一个公式
        a-(int)a/b*b
        */
        System.out.println(10%5);//即可以套公式来计算
    }
}

比较运算符

public class Dome {
    public static void main(String[] args) {
       //> < >= <=
        /*比较运算符的返回值是一个boolear类型*/
    }

赋值运算符

public class Dome {
    public static void main(String[] args) {
       //+= -= *= /= %= = 在运算时会自动的类型转换
    }

逻辑运算符

public class Dome {
    public static void main(String[] args) {
       /*
       逻辑或 |,逻辑与 &
       短路或 ||,短路与 &&
       他们的返回值是固定的
       |这个是只要有一个true就返回true否则false
       &这个是必须要两个true才能返回true
       短路与不短路的区别:
       */
        System.out.println(4>3|1<3);//返回是true
        //先比较4》3 true 在比较1《3 true 则返回true
        System.out.println(3>5&&1<6);//返回的是false
        //短路是只要比较第一个值即可,3》5 false 则返回false
    }
}

位移运算符

public class Dome {
    public static void main(String[] args) {
       //左位移<<,右为移>>,无符号右位移>>>
        /*
        2的二进制:0010
        将2<<3: 0001 0000(相当于*2^n)
        36>>3:0000 0100(相当于/2^n)
        >>> 无符号右移是可以改变符号的
        */
        //原码,反码,补码
        /*
        计算机底层是以补码的型式存在
        计算机表现是以原码的形式表现
        正数的原码,反码,补码都是一样的
        例如:
        负数则不同(计算机最一高位是1代表负号)
        -12的二进制是:
        原码:10001100(负数的原码是高位补1)
        反码:11110011(原码取反)
        补码:11110100(反码+1)
        */
        System.out.println();
    }
}

三目运算符

public class Dome {
    public static void main(String[] args) {
       //表达式 ? true : false;
        //表达式为真的即返回true,则表达式为假的则返回false
        System.out.println(5 > 2 ? 'a' : 'b');
    }
}

控制结构

if… else…

if(表达式){
    代码语句...
}
if(表达式){
    语句...
}else{
    表达式不满足则执行此处...
}
//可以有多种情况的时候
if(){
    
}else if(){
    
}else if(){
    
}...
else{
    
}
//if...else可以嵌套使用但是不建议嵌套太多

public class Dome {
    public static void main(String[] args) {
       //这个用于复制的
        if(5 > 3){
             System.out.println(true);
        }
        if(5 > 7){
             System.out.println(true);
        }else{
             System.out.println(false);
        }
        int a = 2;
        if(a > 0){
             System.out.println(true);
        }else if(a < 0){
             System.out.println(false);
        }else{
             System.out.println("error");
        }
    }
}

switch … case …

switch(){
    case 1 :
    语句块...;
    break;
    case 2 :
    语句块...;
    break;
    ...
    default
    语句...;
}
switch里面的参数是一个固定的值 如果满足case N 则运行caseN下面的代码一直到遇见break则退出
目前switch里面可以放 int String enum 

public class Dome {
    public static void main(String[] args) {
       //这个用于复制的
        System.out.println();
        int a = 1;
        switch(a){
            	case 1 :
                System.out.println(1);
                break;
                case 2 :
                System.out.println(2);
                default
                System.out.println(0);
        }
    }
}

for(;😉{}

for(int i = 0; i < 9; i++){
    语句
}
for循环是先执行 int i = 0;在判断 i 是否 < 9 是则执行下面语句 在执行 i++不是则退出循环

public class Dome {
    public static void main(String[] args) {
       //这个用于复制的
        System.out.println();
        for(int i = 1; i <= 9; i++){
             System.out.println(i);
        }
    }
}

while()… 与 do {} while()

while(表达式){
    语句...;
    递增...
}
do{
    语句...
    递增...
}while(表达式)
这两个的区别是do while 是必须要执行一次的

public class Dome {
    public static void main(String[] args) {
       //这个用于复制的
        System.out.println();
        int i = 1;
        while( i <= 9){
          System.out.println(i);
          i++;
        }
        int a = 1
        do{
          System.out.println(a);
          a++;
        }while(i <= 9)
    }
}

break 与 continue,return

break 是退出语句退出整个循环
continue 是退出循环中的单个循环
return 是退出方法

数组

一维数组

什么是数组?
可以存储多个一样类型的数据,以下标来取数组的第一个下标是0
如何定义数组?
int arr [] = {1,2,3};
arr[0] = 1,arr[1] = 2,arr[2] = 3
int arr [] = new int[3];
表示创建长度是3的int类型的数组默认值是0你可以赋值,如上
数组其他类型的默认值
int 0,double 0.0,boolean false,char null,new null
如何打印数组,数组的扩容,数组的减少,数组反转,数组排序

数组的内存图
在这里插入图片描述

public class Dome {
    public static void main(String[] args) {
       //数组扩容
        int arr [] = {1,2,30};
        int newArr[] = new int[arr.length + 1];
        for(int i = 0; i < arr.length; i++){
            newArr[i] = arr[i];
        }
        arr = newArr;
        //打印数组
       	for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + "\t");
        }
        
        //数组的减少
        int arr [] = {1,2,30};
        int newArr[] = new int[arr.length - 1];
        for(int i = 0; i < newArr.length; i++){
            newArr[i] = arr[i];
        }
        arr = newArr;
        //打印数组
       	for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + "\t");
        }
        //数组的反转
        int arr [] = {1,2,30,14,59,45};
        int temp = 0;
        for (int i = 0; i < arr.length/2; i++ ) {
            temp = arr[i];
            arr[i] = arr[arr.length - i -1];
            arr[arr.length - i -1] = temp;
        }
        
        for (int i = 0; i < arr.length; i++ ) {
            System.out.print(arr[i] + "\t");
        }
        //数组的排序(冒泡排序)
        int arr [] = {1,2,30,14,59,45,55,108,5,11,12};
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++ ) {
            for (int j = 0; j < arr.length - 1 -i ; j++) {
                if(arr[j] < arr[j + 1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++ ) {
            System.out.print(arr[i] + "\t");
        }
    }
}

二维数组

二维数组:
int arr [][] = {{1,2},{6,1},{0,2}};
int newArr [][] = new int [1][3];
二维数组是:数组里面套一个数组

多维数组

即是数据里面套数组在套数组

面向对象

oop面向对象,即把一个物体当做对象,万事万物即对象

class : 类
我们写java源文件时里面都会写到class 这一个class就是一个类例如:
public class Dome {} Dome类
class Dog{} Dog类
如何使用这个类,我们叫创建对象
Dog d = new Dog(); //这样我们就可以去使用这个Dog类了

属性

属性是一个类里面的特征,例如一个Dog类他有什么属性?名字,年龄,颜色 这些就可以用属性来体现
class Dog{
    String name;
    int age;
    String color;
}

class Dog{
    //属性有默认值,与数组相同
    String name;
    int age;
    String color;
}
public class Dome {
     public static void main(String[] args) {
         //给属性赋值
         Dog d = new Dog();
         d.name = "小红";
         d.age = 12;
         d.color = "红色";//这样属性就是有值了
    }
}

方法

方法即类的行为,如何表现方法
public static void main(Sting[] args){return 返回值类型;(void是空)}//这就是一个方法
权限修饰符 特征修饰符 返回值 方法名(参数列表...){ 返回值类型;}

// 写一个方法取两个数的最大值
public class Dome {
    public static void main(String[] args) {
    //调用这个方法
    UtilsMax u = new UtilsMax();
    System.out.println(u.Max(2,3);
    }
}
class UtilsMax {
    /*
    思路分析
    返回值:int
    方法名:Max
    参数:int a,int b
    */
    public int Max(int a,int b){
        return a > b: a ? b; 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值