java基础

2021/6/16 12:22:58

本文主要是介绍java基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基本数据类型

基本数据类型:布尔类型(true,false),整数类型(byte,short,int,long),字符类型(char),浮点类型(float,double)。

  • 1字节内存,占8位:byte
  • 2字节内存,占16位:char,short
  • 4字节内存,占32位:int,float
  • 8字节内存,占64位:long,double

类型精度从低到高:byte short char int long float double

类型低的赋值给级别高的变量时,系统自动完成类型转换。例如:foloat=100。反之,必须使用类型转换运算。

格式如下:(类型名)要转换的值
例如:int x=(int)34.65

注意:String,Integer类型不是基本数据类型。

Random随机数

Random random=new Random()

类型 方法
int nextInt(int bound) 返回伪随机的,均匀分布 int值介于0(含)和指定值(不包括),从该随机数生成器的序列绘制。
float nextfloat() 返回下一个伪随机数,从这个随机数发生器的序列中 0.0和 1.0之间的 float值 0.0分布。
double nextDouble() 返回下一个伪随机数,从这个随机数发生器的序列中 0.01.0之间的 double0.0分布。
int nextInt() 返回下一个伪随机数,从这个随机数发生器的序列中均匀分布 in值。
//随机数对象
Random random=new Random();
//random.nextInt(3)表示[0-2]或者[0-3)
int x=random.nextInt(3)+5;//[5-7)

Math.random()

Math.random()是用于随机生成一个[0.0, 1.0) 的double类型随机数

我们可以把他乘以一定的数,比如说乘以100,他就是个100以内的随机

int ran = Math.random()*100

currentTimeMillis()

System类中有一个currentTimeMillis()方法

这个方法返回一个从1970年1月1号0点0分0秒到目前的一个毫秒数,返回类型是long,我们可以拿他作为一个随机数,我们可以拿他对一些数取模,就可以把他限制在一个范围之内啦

时间格式化

Date date = new Date();
String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); 
System.out.println(sdf.format(date));

File

创建文件夹(目录)

  • mkdirs()可以建立多级文件夹

  • mkdir()只会建立一级的文件夹

     File folder = new File("d:/china/my");
    
    //直接创建多级目录
    
     folder.mkdirs(folder)
         
    //只有存在d:/china才能创建my文件夹
         
     folder.mkdir(folder)
    
public class CreateFolder {
        public static void main(String[] args) {
            boolean isCreateFolder=false;
            //文件夹创建
            File folder = new File("d:/china/my");
            //判断文件夹是否存在
            if(!folder.exists()){
                //创建文件夹
                isCreateFolder= folder.mkdirs();
            }
            else{
                System.out.println("文件夹已存在");
            }
            System.out.println(isCreateFolder?"目录创建成功":"目录创建失败");
        }
}

创建文件

public class CreateFile {
    public static void main(String[] args) throws IOException {
        //文件路径
        File file = new File("d:/china/h.java");
        //文件上级目录 
        String ParentPath = file.getParentFile().toString();
        //判断目录是否存在
        if (!file.getParentFile().exists()) {
            //创建目录
            boolean mkdirs = file.getParentFile().mkdirs();
            System.out.println(mkdirs ? "创建目录成功" : "创建目录失败");
        }
        if (file.exists()) {
            System.out.println("文件存在");
            boolean delete = file.delete();
            System.out.println(delete ? "文件删除成功" : "文件删除失败");
            boolean newFile = file.createNewFile();
            System.out.println(newFile ? "文件创建成功" : "文件创建失败");
        } else {
            boolean newFile = file.createNewFile();
            System.out.println(newFile ? "文件创建成功" : "文件创建失败");
        }
    }
}

数组和集合互转

数组转集合

  • toArray ( )

    List<Integer> pod = new ArrayList<Integer>(); 
    for(int i = 0; i < 5; i++){
      pod.add(i);
    }
    Integer[] t=(Integer[])pod.toArray();
    
  • toArray ( T[ ] a )

    List<Integer> pod=new ArrayList<Integer>();
    for(int i=0;i<5;i++){
        pod.add(i);
    }
    Integer[] t=new Integer[pod.size()];
    pod.toArray(t);
    

集合转数组



这篇关于java基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程