java的基本程序设计结构

注释方法

1.每行前面//

2/*和*/将一段比较长的注释括起来(不能嵌套,如果程序当中有*/,无法使用)

3./**一堆注释*/会自动生成文档。

类型

char类型用来表示单个字符。

'A' 编码为65所对应的字符常量。

"A"是包含字符A的字符串

Java中int和boolean不可以互换

声明一个变量之后,必须用赋值语句对变量进行显示初始化

int a;
System.out.println(a);//error a未初始化


final(该变量只能被赋值一次,一旦赋值之后,就不能再更改) 指示常量

运算

x+=4 等价于 x=x+4(运算符放在赋值号的左侧)

前缀方式++n先进行加1操作。后缀方式则使用变量原来的值

public class prefix {
public static void main(String[] args)
{
    int n=7;
    int m=7;
    int a=2*++n;//2*8
    int b=2*m++;//2*7
    System.out.println("a="+a+"b="+b);
}
}

a=16 b=14.

substring(a,b)b是从你第一个不想复制的位置开始算的。优点:容易计算字串长度。

String s="Hello world";
String ss=s.substring(0,3)+"p!";// 长度3-0=3 Help!
System.out.println(ss);


 

相等

s.equal(t);

s和t可以是传字符串变量 也可以是字符串常量。

eg: "hello".equals(h)

String h="hello";
if("hello".equals(h))
System.out.println("true");

true.

equalIgnoreCase 忽略大小写判断两个

public class prefix {
public static void main(String[] args)
{
    String h="hello";
    if("Hello".equalsIgnoreCase(h))
    System.out.println("True");
    else System.out.println("False");
    //true;
}
}

如果==判断的地址是否相等

/**C++的string类重载了==运算符以便检测字符串内容的相等性,JAVA没有重写,其操作方式类似于指针。

*/

/**c使用srcmp函数。相当于java comapreTo

if(s.compareTo("Hello")==0)

 */

空串&Null串

空串""是长度未0的字符串。

检查一个字符串是否为空

if(str.length()==0);

if(str.equals(""));

null表示目前没有任何与该变量关联

要检查一个字符串是否为null:if(str=null);

codePointCount得到实际的长度,即代码点的数量

charAt(n)返回n的代码单元,n介于0~s.length()-1之间

public class prefix {
public static void main(String[] args)
{
    String h="hello呵";
    int cpCount=h.codePointCount(0, h.length());//6
    char first =h.charAt(0);//h
    char last=h.charAt(5);//
}
}

读取文件

Scanner in=new Scanner(Paths.get"myfile.txt")

 如果文件名中包含反斜杠,就要记住在每个反斜杠前加一个额外的反斜杠:"c:\mydirectory\myfile.txt"

int x;
Scanner in=new Scanner(System.in);
x=in.nextInt();
这样就可以输入一个数字并且赋值给x

写入文件

PrintWriter out=new PrintWriter("myfile.txt")
原文地址:https://www.cnblogs.com/Ljj-Nancy/p/5753684.html