java语言基础(有c语言基础)

JAVA语言

jdk+记事本编译

编译javac Hello.java

执行java Hello

数据类型

byte b=123;//整型8位最大值是2的7次减一,第一位是符号位

short s=32156;//最大是2的15次-1

int i=101;//31

long l=123;63

float s=3.14;

double d=3.14;

boolean ok=true;

char c='a';

3.14默认double 在后面加f

float s=3.14f;(F不区分大小写

java无符号

字符

可以赋值int

可以赋值‘哈’

'\u0043'//u ni kou de字符集

java里面1字符是两个字节能存储汉字

数组

int[] a=new int [10];//定义大小为10的int数组
//java创建新的必须new一个
int[] a=(这里也可以加new int[]){1,2,3,4,5};//赋值
​
int [][]a=new int[3][4];//二维数组
​
for(int i=0;i<a.length;i++)
{
    Stystem.out.println(a[i]);
}打印数组

字符串

String s="Hello";//String本质上是一个类而不是基本类型
System.out.println(s);
System.out.println(s.substring(1,4));//截取部分输出
for(int i=0;i<s.length();i++)
{
    System.out.println(s.charAt(i));
}

字符串比较

Stirng s1=new String("Hello");//这个是程序的本质①
String s2="Hello";//这个是简写②
Stirng s1=new String("Hello");
Stirng s2=new String("Hello");
String s3="Hello";

①new赋值是不可以相互共享

②等于号赋值的是可以相互共享

new出来的东西一定在堆上

java里面类这个类型相当于C中的mylok

字符串内容比较(不是地址

System.out.println(a==b);//这是等号比较,比较的是地址
System.out.println(s1.equals(s2));

子类类型的变量可以赋给父类

equals是object

创建类必须先new一个(相当于初始化)

student =new student();//相当于一个初始化

对象必须在堆里创建不能在栈里面

private int id;

设置私有保护以后通过方法来修改和获取类里面的数据

类的名字可以相同,但是类不能完全相同(参数

java内存释放以后自动清理,不需要用手动

静态块new多次只执行一次

普通块new一次执行一次

静态变量只有一份内存

常量

java的final相当于c的const起保护作用

package cn.edu.hit;//这个是包
cn.edu.hit.Student s=new cn.edu.hit.Student();//调用包中的Student

课程代码

class Student{
    private int id;
    private String name;
    public Student(){
    }
    public Student(int id){
        this.id=id;
    }
    public Student(String name){
        this(0);
        this.name=name;
    }
    public Student(int id,String name){
        this(name);
    }
​
    public void finalize(){
        System.out.println("Goodbye!");
    }
​
    public int getId(){
        return this.id;
    }
    public void setId(int id){
        this.id=id;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name=name;
    }
}
​
public class Hello{
    public static void main(String[] args){
        Student s=new Student();
        s.setId(1);
        s.setName("Alice");
        System.out.println(s.getId());
        System.out.println(s.getName());
        s=null;//手动让他变成垃圾
        System.gc();//强制清理
    }
}

类的继承

public class Student extends Preson//这就是一种继承

子类的构造器一定会调用父类的构造器(默认无参构造器)

super();//主动调用父类构造器
super.;//主动调用父类方法
this();//主动调用自己构造器
this.;//主动调用自己方法

!!!当给子类定义一个有参构造器,必须也给他同时定义一个无参构造器

构造器无法继承,必须用super调用

类数组

ss[0]=new Student(" "," "," ");

这叫初始化

排序

in
Arrays.sort(ss);

抽象类

public abatract class Person{
​
}

抽象类无法被实例化,不能new,只能作为父类进行继承

接口

class Dog implements Animal;

接口对于类型的转换有一定作用,接入接口也可以冲掉原有函数

异常处理

public static int div(int x,int y)throws{
    try{
    
    }
    catch(){
            throw 
    }
    
}
finally{//最后一定会执行
​
}

try throws throw catch finally

文件处理

1.创建文件:你可以使用File类的构造函数来创建一个文件对象,然后使用createNewFile()方法来创建实际的文件。

File file = new File("path/to/file.txt");
file.createNewFile();

2.写入文件:你可以使用FileWriterBufferedWriter类将数据写入文件。这些类提供了多种写入文件的方法,例如write()append()

FileWriter writer = new FileWriter("path/to/file.txt");
writer.write("Hello, World!");
writer.close();

3.读取文件:你可以使用FileReaderBufferedReader类来读取文件的内容。这些类提供了多种读取文件的方法,例如read()readLine()

FileReader reader = new FileReader("path/to/file.txt");
int character;
while ((character = reader.read()) != -1) {
    System.out.print((char) character);
}
reader.close();

4.删除文件:你可以使用File类的delete()方法来删除文件。

File file = new File("path/to/file.txt");
file.delete();

这只是文件操作中的一些常见方法示例,Java提供了更多的类和方法来处理文件和目录操作。记得在进行文件操作时要处理可能出现的异常,并使用适当的异常处理机制。

二进制文件操作

对于二进制文件操作,你可以使用Java的InputStreamOutputStream类来读写二进制数据。下面是一些常见的操作方法:

1.读取二进制文件:你可以使用FileInputStream类来读取二进制文件的内容。

try (InputStream inputStream = new FileInputStream("path/to/file.bin")) {
    int bytesRead;
    byte[] buffer = new byte[1024];
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        // 处理读取的数据
    }
} catch (IOException e) {
    e.printStackTrace();
}

2.写入二进制文件:你可以使用FileOutputStream类将二进制数据写入文件。

try (OutputStream outputStream = new FileOutputStream("path/to/file.bin")) {
    byte[] data = {0x00, 0x01, 0x02, 0x03};
    outputStream.write(data);
} catch (IOException e) {
    e.printStackTrace();
}

3.使用BufferedInputStreamBufferedOutputStream:如果需要提高读写的效率,你可以使用BufferedInputStreamBufferedOutputStream类对输入输出流进行缓冲。

try (InputStream inputStream = new BufferedInputStream(new FileInputStream("path/to/file.bin"));
     OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("path/to/file.bin"))) {
    // 读写操作
} catch (IOException e) {
    e.printStackTrace();
}

请注意,在处理二进制文件时,你需要特别小心处理数据的格式和顺序,以确保正确解析和生成二进制数据。此外,你还需要了解二进制文件的结构和特定格式的规范,以正确处理其数据。

猜你喜欢

转载自blog.csdn.net/qq_34168477/article/details/131736203