Java基本的程式設計結構(一)

來源:互聯網
上載者:User
前言:雖然說學過設計模式,J2EE,這個學期才開始學Java,呵呵,有點顛倒了,但是還是要從基本的抓起。hoho~~
(一)一個簡單的java應用程式
Package edu.ynu.java.lession1
/*
The simplest Java program
*/
public class FirstJavaProg
{
    public static void main(String[] args)
    {
        // System.out is the standard output stream.
        System.out.println("Hello!");
    }
}

(二)注釋
// This is my first Java program

(三)資料類型
byte (1 bytes, -128 … 127)
short (2 bytes, -32,768 … 32,767)
int (4 bytes, -2,147,483,648 … 2,147,483,647)
long (8 bytes)
Integer literals can be specified in several bases:
    Decimal — -99 or 32174
    Octal — 012 or 07
    Hexadecimal — 0xff or 0XABCDEF01
float
    4 bytes
    6.5 significant digits
    ±3.4028E+38F
    denoted by ‘F’ or ‘f’ suffix
double
    8 bytes
    15 significant digits
    ±1.7977E+308
    denoted by ‘D’ or ‘d’ suffix (or no suffix)

    if (Double.isNaN(x)) // check whether x is "not a number"

boolean
char

(四)變數
賦值和初始化
常量
Example:
public class Constants
{
   public static void main(String[] args)
   {
     final double CM_PER_INCH = 2.54;
      double paperWidth = 8.5;
      double paperHeight = 11;
      System.out.println("Paper size in centimeter: "
         + paperWidth * CM_PER_INCH + " by "
         + paperHeight * CM_PER_INCH);
   }
}

(五)運算子
Arithmetic +, -, *, /, and %.
Prefix and postfix ++ and --.
Boolean ==, !=, <, <=, >, >=, &&, and ||.
Bitwise &, |, ^, and ~.
Arithmetic shift << and >>.
Logical shift >>>.

Java logical operators
&&   (conditional AND)
&     (boolean logical AND)
||   (conditional OR)
|     (boolean logical inclusive OR)
^     (boolean logical exclusive OR)
!     (logical NOT)

數學函數和常量
Constants (e.g, Math.PI)
Functions (e.g., Math.sin())
數實值型別之間的轉換

強制類型轉化
double x = 9.997;
int nx = (int)Math.round(x);

括弧和運算子層級
如記不得可以使用括弧

枚舉類型
enum Size{SMALL, MEDIUE, LARGE, EXTRA——LARGE};
Size s = Size.SMALL;

註:變數用小寫字母開頭,常量和類名用大寫字母開頭

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.