3、Java基础部分总结

开发环境:

https://www.cnblogs.com/xiadongqing/p/5414649.html
https://jingyan.baidu.com/article/e75aca85b29c3b142edac6a8.html
安装eclipese
https://www.cnblogs.com/smivico/p/6256313.html
eclipese新建一个工程项目
https://baijiahao.baidu.com/s?id=1593000414240065847&wfr=spider&for=pc

1、Java 简介

Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。

Java可运行于多个平台,如Windows, Mac OS,Linux,及其他多种UNIX版本的系统。

主要特性:

简单:与C语言和C++语言很接近,不使用指针,而是引用。

面向对象:提供类、接口和继承等面向对象的特性。

安全

运行环境:JDK + Eclipse

例:
package Test_hello;

public class Hello {                       //类

         public static void main(String[] args) {    //主程序

        System.out.println("Hello World");  // 打印 Hello World

         }

}

:源文件名必须和类名相同;大小写敏感。

注释://单行注释

      /*多行注释*/

3、数据类型

Boolean、char、String、int、float、double、long 、short、void

#define定义常量

#define LENGTH 10 

const声明常量

const int  LENGTH = 10;  //通常把常量定义为大写字母形式

static 存储类

tatic 修饰符也可以应用于全局变量。当 static 修饰全局变量时,会使变量的作用域限制在声明它的文件内

static int count = 10; /* 全局变量 */

extern 存储类:

extern 存储类用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。

extern int a, b;   //全局变量

extern void write_extern();  //全局函数变量

int a, b;   //局部变量

 

4、变量类型

局部变量:

类的方法中的变量。

局部变量只在声明它的方法、构造方法或者语句块中可见;

局部变量是在栈上分配的。

局部变量没有默认值,所以局部变量被声明后,必须经过初始化(赋值),才可以使用。

例:

package Test_hello;

public class Hello {         //类

         public void pupAge() {  //方法

         int age = 0;           //局部变量初始化(必须)

         age = age + 7;

         System.out.println("年龄是:" +age);  //打印

         }

         public static void main(String[] args) {   //主程序

                  Hello test = new Hello();         //实例化

        test.pupAge();

         }

}

 

实例变量

独立于方法之外的变量,不过没有 static 修饰。

例:

package Test_hello;

public class Hello {           //类

         public String name;      // 这个实例变量对子类可见

         private double salary;   // 私有变量,仅在该类可见

        

         public Hello(String empName) {   //声明实例变量

         name = empName;         //在构造器中对name赋值

         }

         public void setSalary(double empSal) {  //设定salary的值

                  salary = empSal;

                  }

         public void printEmp() {       //方法:打印

                  System.out.println("名字:" + name);

                  System.out.println("年龄是:" + salary);

                  }

         public static void main(String[] args) {       //主程序

                  Hello empOne  = new Hello("TOM");

        empOne.setSalary(6);

        empOne.printEmp();

         }

}

类变量(静态变量)

独立于方法之外的变量,用 static 修饰。

例:

package Test_hello;

public class Hello {

         private static double salary;   //salary是静态的私有变量

         public static final String DEPAR = "开发人员";

        

         public static void main(String[] args) {    //主程序                        

                  salary = 100;

                  System.out.println(DEPAR + "平均工资:" + salary);

         }                                                                                                                    

}

输出:

开发人员平均工资:100.0

5、修饰符

default (即缺省,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。

private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。

public : 对所有类可见。使用对象:类、接口、变量、方法。

protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。

4、Java 数学运算

算术运算符

+、-、*、/、%、++、--

关系运算符

==、!=、>、<、>=、<=

逻辑运算符 (常用于条件语句)

&&、||、!  

位运算符

&、|、^、<<、>>

赋值运算符

=、+=、-=、*=、/= 等

条件运算符:?:

例:

int a , b;

a = 1;

b = (a == 1) ? 20 : 30;  // 如果a等于1成立,则设置b为20,否则为30。

数学运算函数

弧度角的正弦、余弦、正切:double cos(double); double sin(double);double tan(double);

自然对数:double log(double);

x 的 y 次方: double pow(double x, double y);

平方总和的平方根: double hypot(double, double);

平方根: double sqrt(double);

绝对值: int abs(int); 等

随机数:random()

:以上函数使用时,均需加Math.  ,如Math.sin(Math.PI/2)

5、循环

while 循环、for 循环do...while 循环嵌套循环

循环控制语句:

break 语句、continue 语句

例:

package Test_hello;

public class Hello {

    public static void main(String[] args) {    //主程序

        int i = 1;

        while(i<=3) {                 //while循环语句

        System.out.println( "value of x :" + i);

        i++;

        }

    }

}

6、判断

if 语句、if...else 语句、if...else if...else、嵌套 if 语句switch 语句

例:

package Test_hello;

public class Hello {

    public static void main(String[] args) {    //主程序

        int x = 30;

          if( x == 10 ){        //if...else if...else 语句

             System.out.print("Value of X is 10");}

          else if( x == 20 ){

             System.out.print("Value of X is 20");}

          else if( x == 30 ){

             System.out.print("Value of X is 30");}

          else{

             System.out.print("无此值");

          }

    }

}

7、字符串

char[] a = {'H','e'};   String b = new String(a);  //用String类进行转换

String a = "Hello";

连接字符串 s2 到字符串 s1 的末尾:

String a = "Hello";

String b = " World";

String c = a.concat(b);   //调用 concat

返回字符串 s1 的长度:

String a = " Hello ";

int len = a.length();     //调用length

8、数组

声明数组变量:

double[] a;         // 首选的方法

double a[];         //  效果相同,但不是首选方法

初始化数组:

double[]  a= {1.0, 2.0, 3.6, 7.0, 5.0};  //一维数组

int[] a = new int[10]; 

int[][] a = { {0, 1, 2} , {4, 5, 6} , {8, 9, 10} };   //二维数组

访问数组元素:

double b = a[1];  // b为2.0

int m = a[2][3];  //m为11

计算维度:

计算一维数组的长度:int len = a.length;

9、文件处理

打开文件、关闭文件、写入文件、读取文件

例:
package Test_hello;

import java.io.*;      //包含了所有操作输入、输出需要的类。

public class Hello {

          public static void main(String[] args) throws IOException {

                 File f = new File("a.txt");

                 FileOutputStream fop = new FileOutputStream(f);

                 // 构建FileOutputStream对象,文件不存在会自动新建

          

                 OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");

                 // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

          

                 writer.append("中文输入");

                 // 写入到缓冲区

          

                 writer.append("\r\n");

                 // 换行

          

                 writer.append("English");

               // 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入

          

                 writer.close();

                 // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉

          

                 fop.close();

                 // 关闭输出流,释放系统资源

          

                 FileInputStream fip = new FileInputStream(f);

                 // 构建FileInputStream对象

          

                 InputStreamReader reader = new InputStreamReader(fip, "UTF-8");

                 // 构建InputStreamReader对象,编码与写入相同

          

                 StringBuffer sb = new StringBuffer();

                 while (reader.ready()) {

                     sb.append((char) reader.read());

                     // 转成char加到StringBuffer对象中

                 }

                 System.out.println(sb.toString());

                 reader.close();

                 // 关闭读取流

          

                 fip.close();

                 // 关闭输入流,释放系统资源

             }

}

猜你喜欢

转载自www.cnblogs.com/ai-learning-blogs/p/13388060.html