---
title: "Java基本的程序设计结构"
author: "xuxujing"
date: "2009-08-05T22:52:06Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-08-06T19:09:07Z"
id: 441072
url: "https://xujava.iteye.com/blog/441072"
---

1.注释

    1)   //,行注释 

    2)   /* \*/,段注释

    3)   /\** \*/,文档注释

 

2.数据类型

    1)   整型  int(4字节),short(2),long(8),byte(1)

    2)   浮点  float(4),double(8)

          浮点数值不适用于禁止出现舍入误差的金融计算中。如果需要在数值计算中不含有舍入误差，应该使用BigDecimal类。

    3)   char类型  \\b(退格),\\t(制表),\\n(换行),\\r(回车),\\"(双引号),\\'(单引号),\\\\(反斜杠)

    4)   boolean类型

 

3.变量

 

4.运算符

 

5.字符串

    Java字符串就是Unicode字符序列。

    1)   字串

       

```
// substring中从0开始计数，直到3为止，但不包含3
String greeting="Hello";
String s=greeting.substring(0,3);//s="Hel";
```

 

   2)   拼接

       

```
String expletive="Expletive";
String PG13="deleted";
String message=expletive+PG13;//message="Expletivedeleted";

int age=13;
String rating="PG"+age;//rating="PG13";
```

 

   3)   不可变字符串

   

```
greeting="Hello";
```

          字符串"Hello"永远包含字符H,e,l,l和o的代码单元序列，而不能修改其中的任何一个字符。当然可以修改字符串变

    量greeting,让它引用另外一个字符串。

 

   4)检测字符串是否相等

   

```
 "Hello".equals(greeting);
 "Hello".equalsIgnoreCase("hello");//不区分大小写
 //一定不能使用==运算符检测两个字符串是否相等
 //这个运算符只能确定两个字符串是否放置在同一个位置上
```

    5)   代码点与代码单元

    6)   字符串API

         

```
char charAt(int index)
//返回给定位置的代码单元
int codePointAt(int index)//5.0
//返回从给定位置开始或结束的代码点
int offsetByCodePoints(int startIndex,int cpCount)//5.0
//返回从startIndex代码点开始，位移cpCount后的代码点索引
int compareTo(String other)
//按照字典顺序，如果字符串位于other之前返回负数，之后返回正数，相等返回0
boolean endsWith(String suffix)
//如果字符串以suffix结尾，返回true
boolean equals(Object other)
//如果字符串与other相等，返回true
boolean equalsIgnoreCase(String other)
//如果字符串与other相等（忽略大小写），返回true
int indexOf(String str)
int indexOf(String str,int fromIndex)
int indexOf(int cp)
int indexOf(int cp,int fromIndex)
//返回与字符串str或代码点cp匹配的第一个子串的开始位置。这个位置从索引0或
//fromIndex开始计算。如果原始串中不存在str，返回-1
int lastIndexOf(String str)
int lastIndexOf(String str,int fromIndex)
int lastIndexOf(int cp)
int lastIndexOf(int cp,int fromIndex)
//返回与字符串str或代码点cp匹配的最后一个子串的开始位置。这个位置从原始串
//尾端或fromIndex开始计算。
int length()
//返回字符串的长度
int codePointCount(int startIndex,int endIndex)//5.0
//返回startIndex和endIndex-1之间的代码点数量。没有配成对的代用字符将计
//入代码点
String replace(CharSequence oldString,CharSequence newString)
//返回一个新字符串。这个字符串用newString代替原始字符串中所有oldString。
//可以用String或StringBuilder对象作为CharSequence参数
boolean startsWith(String preffix)
//如果字符串以preffix字符串开始，返回true
String substring(int beginIndex)
String substring(int beginIndex,int endIndex)
//返回一个新字符串。从beginIndex到串尾或endIndex-1的所有代码单元
String toLowerCase()
//返回一个新字符串，将原始字符串中的所有大写字母改成小写字母
String toUpperCase()
//返回一个新字符串，将原始字符串中的所有小写字母改成大写字母
String trim()
//返回一个新字符串，将删除字符串头部和尾部的空格
```

  6.输入输出

       

```
Scanner in=new Scanner(System.in);

String name=in.nextLine();//读一行

String name=in.next();//读取一个单词，以空格分隔

int age=in.nextInt();//读整数,nextDouble..

//读密码
Console cons=System.console();
String username=cons.readLine("User name:");
char[] passwd=cons.readPassword("Password:");//只能在命令行执行
```

 

```
boolean hasNext()//检测输入中是否还有其他单词
boolean hasNextInt()
boolean hasNextDouble()
```

 

    7.格式化输出