1 / 102

第二讲 . Java 基础

第二讲 . Java 基础. 内容. Java 平台 Java 程序结构 Java 注释 Java 语法 Java 程序语言的数据类型 运算符 字符串 标准输入输出 控制结构 大数处理 数组 编码过程. 1. Java 平台. debug. “WRITE ONCE, RUN ANYWHERE!”. pretty portable. Program in Java. Java Compiler. Java Bytecode. Java Virtual Machine. Java Virtual Machine. App1. App2.

rasha
Download Presentation

第二讲 . Java 基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第二讲. Java基础

  2. 内容 • Java平台 • Java程序结构 • Java注释 • Java语法 • Java程序语言的数据类型 • 运算符 • 字符串 • 标准输入输出 • 控制结构 • 大数处理 • 数组 • 编码过程

  3. 1. Java 平台 debug “WRITE ONCE, RUN ANYWHERE!” pretty portable Program in Java Java Compiler Java Bytecode Java Virtual Machine Java Virtual Machine

  4. App1 App2 App3 App4 App5 Java Virtual Machine (Java 虚拟机) Windows Linux OS X Solaris Linux Intel PowerPC SPARC

  5. 虚拟机是 Java 程序运行的统一平台 • Java Virtual machine abstracts away the details of the underlying platform and provides a uniform environment for executing Java “byte-code” • The Java compiler (javac) compiles Java code into byte-code (编译程序编译 Java 源程序为字节码) • Bytecode is an intermediate form which can run on the JVM • JVM does the translation from byte-code to machine-code in a platform dependent way.

  6. Core Language (核心语言) • Ints, array, objects, loops and conditionals • Moderately sized language • Can run on small devices • Class Libraries (类库) • This is where the power of Java really emerges • String, ArrayList, HashMap, … • Networking, Graphics, XML, Database connectivity, Web Services…. • Re-use at it’s best (so far).

  7. 2. Java程序结构

  8. 一个Java程序的基本框架结构 • Java Application程序: 例: HelloWorld.java public class HelloWorld { public static void main (String[] args) {System.out.println(“Hello! World!”);} } • Java程序由一个或多个独立的类组成,但其中必须有一个公有类(如:HelloWorld),而且源代码文件必须与这个公有类的名字相同(如:HelloWorld.java) • Java的类可以由一个或多个方法组成,其中公有类中的main方法可用作程序运行的入口。

  9. Java 程序的编辑、编译和运行过程 Java程序编译通过,会自动生成一个包含了这个源程序字节码的同名.class文件。该文件可以通过Java虚拟机运行。 • 开始使用JDK: • javac HelloWorld.java 编译器  编译出 HelloWorld.class • java HelloWorld 解释器运行主类

  10. Java Programs (Java 程序) • You have to create a class! (创建一个类) • You run the class (对类进行操作) • for this to work, the class must have a method named main() that is declared as public static void main( Strings[] args) • if you don't do this right, you see something like this when you try to run a class: Exception in thread "main" java.lang.NoSuchMethodError: main

  11. public static void main() • public: This method can be accessed (called) from outside the class. • static: This method does not require that an object of the class exists. static methods are sort-of like "global" methods, they are always available (but you have to use the class name to get at them). • void: no return value.

  12. main(Strings[] arg) • main() will be passed an array of Strings corresponding to any command line parameter values. • If you ran a program (class) like this: java Foo hi there dave • then Foo.main() would be passed an array (with length 3) of String objects. The values would be "hi", "there" and "dave". • args[0] is the String "hi", …

  13. 简单的 Java 程序

  14. 撰写规范的程序代码 • // 程序说明 • package包的名称 • import类的名称 • 类修饰符class 类的名称 • 构造方法 • 类成员:方法 (按照以下访问特性次序排列) • public • protected • private protected • private • 类成员:域 (按照以下访问特性次序排列) • public • protected • private protected • private • 静态变量(即使用 static修饰符的。按照以下访问特性次序排列) • public • protected • private protected • private • 常量 (即使用static final修饰符的)

  15. Java 分隔符 • Java分隔符组成: 圆点--“.”、分号--“;”、花括号--“{}”、空格--“” • Java分隔符作用: • Java语句必须以分号作为结束标记 • Java允许用花括号“{}”将一组语句括起来,形成一个语句块(block) • Java程序源代码中各组成部分之间可以插入任意数量的空格,包括换行。

  16. 完整 Java 程序的基本元素 1. 评论/注释语句 (/* */, // etc.) 2. 包含文件 (import java.io.*, …) 3. 赋值语句 (int i, float sum=0) 4. 简单输入/输出语句 (in, out) 5. 算术运算和逻辑操作 (+,-,*,\,) 6. 序列结构 7. 选择结构 (switch ~ case ~break, if ~ else) 8. 循环结构 (do~while, while, for, ) 9. 类和程序调用

  17. 3. Java注释

  18. Java注释 • Java语言中定义了三种注释形式: // 单行注释----注释到行尾 /* 单行或多行注释 */ /** 可以用于文档化处理的单行或多行注释 */ • JDK中提供了一个文档自动生成工具 javadoc,在自定义类中 public 的成员前以/**…*/形式加入的注释内容均可被自动提取到生成的说明文档中。 用法:somepath\javadoc source.java

  19. 4. Java语法

  20. java 语言的关键字 • Java中一些赋以特定的含义、并用做专门用途的单词称为关键字(keyword) • 所有Java关键字都是小写的,TURE、FALSE、NULL等都不是Java关键字 ; • goto和const 虽然从未使用,但也被作为Java关键字保留; • true, false, null虽被用做专门用途,但不是Java关键字;

  21. Class Type: package, class, abstract, interface, implements, native, this, super, extends, new, import, instanceof, public, private, protected, • Data Type: char, double, enum, float, int, long, short, boolean, void, byte, • Control Type: break, case, continue, default, do, else, for, go to, if, return, switch, while, throw, throws, try, catch, synchronized, final, finally, transient, strictfp • Storage Type: register, static • Other Type: const, volatile,

  22. 5. Java程序语言的数据类型

  23. Java数据类型划分 整数类型(byte, short, int, long) 数值型 浮点类型(float, double) 基本数据类型 字符型(char) 布尔型(boolean) 数据类型 类(class) 引用数据类型 接口(interface) 数组

  24. (1)基本数据类型 • Java中定义了四类/八种基本数据类型 • 逻辑型---- boolean • 字符型---- char • 整数型---- byte, short, int, long • 浮点数型---- float, double

  25. 逻辑型 • boolean类型适于逻辑运算,一般用于程序流程控制 • boolean类型数据只允许取值true或false,不可以0或非0的整数替代true和false。 • 用法举例: boolean b = false; if(b==true) { //do something }

  26. 字符型 • char型数据用来表示通常意义上“字符” • 字符常量是用单引号括起来的单个字符 • char c = 'A'; • Java字符采用Unicode编码,每个字符占两个字节,因而可用十六进制编码形式表示 • char c1 = '\u0061'; • Java语言中还允许使用转义字符'\'来将其后的字符转变为其它的含义 • char c2 = '\n'; //代表换行符

  27. 6. Java变量与标识符

  28. 变量 程序执行中数值可变的数据称为变量。变量包括变量名和变量值。 • 变量名  用标识符命名,对应一定数量的内存存贮单元,其单元数视变量类型而定。 标识符  符合一定命名规则的字符串序列。

  29. 变量声明与赋值

  30. 标识符(1) • Java语言中,为各种变量、方法和类等起的名字称为标识符 • Java标识符的命名规则: • 应以字母、下划线、美元符开头 • 后跟字母、下划线、美元符或数字 • Java标识符大小写敏感,长度无限制

  31. Java标识符举例

  32. 7. 运算符

  33. 算术运算 Operator ---------------- + - * / % ---------- ++-- Definition ---------------- Addition Subtraction Multiplication Division (quotient) Division (remainder) ----------------------- Increment Decrement Example---------------------- 3 + 5 2 - 4 Num * 5 Sum / Count Count % 4 ----------------------- Count ++ Count --

  34. 关系运算 Operator ---------------- < <= > >= == != Definition ---------------- Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Example---------------------- Num1 < 5 Num1 <= 5 Num2 > 3 Num2 >= 3 Num1 == Num2 Num1 != Num2

  35. 对象相等性 /: c03:EqualsMethod.java public class EqualsMethod { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } ///:~ //: c03:Equivalence.java public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } ///:~ //: c03:EqualsMethod2.java class Value { int i; } public class EqualsMethod2 { public static void main(String[] args) { Value v1 = new Value(); Value v2 = new Value(); v1.i = v2.i = 100; System.out.println(v1.equals(v2)); } } ///:~

  36. 逻辑运算 Operator ---------------- ! && || Definition ---------------- NOT AND OR Example---------------------- ! ( Num1 < Num2 ) (Num1 < 5 ) && (Num2 > 10 ) (Num1 < 5 ) || (Num2 > 10 ) 注意:只能将AND, OR,NOT施加于boolean型上,否则出错

  37. 位运算符 Operator ---------------- & | ^ ~ Definition ---------------- AND OR EXCLUSIVE OR ONE’S COMPLEENT Example---------------------- 2&3=2 2|3=3 2^3=1 ~2=-3

  38. 位移运算符 Operator ---------------- << >> >>> Definition ---------------- 左移 右移 无正负右移 Example---------------------- i=2;i<<2=8 i=2;i>>2=0 i=-1;i>>>2=1073741823

  39. 赋值运算 Operator ---------------- == += -= *= /= %= Example ---------------- Num = 5 Num += 5 Num -= 5 Num *= 5 Num /= 5 Num %= 5 Meaning---------------------- Store 5 in Num Num = Num + 5 Num = Num - 5 Num = Num * 5 Num = Num / 5 Num = Num % 5

  40. if-else 三元运算符 Operator ---------------- boolean-exp?value0:value Definition ---------------- 评估boolean-exp的值,决定使用value0还是value Example---------------------- i<10?i*100:i*10

  41. String上的+ • Java不允许程序员实现自有的重载运算符 • +字符串连接 • 如果表达式中有String,其他类型转变为String

  42. 转型运算符 • Java允许将任意基本型别转型为另一个任意基本型别,boolean例外 • class型别不允许转型,同一族系,子类对象可以转型为父类对象 publicclass Concatenation { publicstaticvoid main(String[] args) { String mango = "mango"; String s = "abc" + mango + "def" + 47; System.out.println(s); } } /* Output: abcmangodef47 *///:~

  43. 8. 字符串: String

  44. String Class (字符串) • 创建字符串常量String对象:String类的构造函数 public String( ); public String(String value); public String(StringBuffer buffer); public String(char value[] ); 创建Sring类的对象分两步:声明和创建 String s; s=new String(“agah”); or s=“java”

  45. importstatic net.mindview.util.Print.*; publicclass Immutable { publicstatic String upcase(String s) { return s.toUpperCase(); } publicstaticvoid main(String[] args) { String q = "howdy"; print(q); // howdy String qq = upcase(q); print(qq); // HOWDY print(q); // howdy } } /* Output: howdy HOWDY howdy *///:~ Java中字符串是不能被修改的

  46. String Class (字符串) • 字符串常量的操作: public boolean startsWith(String prefix); public boolean endsWith(String suffix); public int length( ) public int indexOf(int ch) public int indexOf(int ch,int fromIndex); public int indexOf(String str); public int indexOf(String str,int fromIndex); public int lastIndexOf(String str); public int lastIndexOf(String str,int fromIndex); public int compareTo(String anotherString); public boolean equal(Object anObject); public boolean equalsIgnoreCase(String anotherString); public String concat(String str); ….

  47. 9. Java标准输入输出简介

  48. Scanner (标准输入) • Scanner – gets values from the standard input and assigns them to Object import java.util.Scanner Scanner stdin = new Scanner( System.in ); stdin.next(), nextFloat(), nextInt(), nextLine()…. hasNext(); hasNextFloat(), ……

More Related