JAVA语言基础

 

一、运算符的优先次序

        表达式的运算按照运算符的优先顺序从高到低进行,同级运算符从左到右进行:
        

优先次序
运算符
1
. [] ()
2
++ -- ! ~ instanceof
3
new (type)
4
* / %
5
+ -
6
>> >>> <<
7
> < >= <=
8
= = !=
9
&
10
^
11
|
12
&&
13
||
14
?:
15
= += -= *= /= %= ^=
16
&= |= <<= >>= >>>=

 

二、字符串处理

 

        1、 字符串的表示

  Java语言中,把字符串作为对象来处理,类String和StringBuffer都可以用来表示一个字符串。(类名都是大写字母打头)

 (1).字符串常量

  字符串常量是用双引号括住的一串字符。
    "Hello World!"

 (
2).String表示字符串常量

  用String表示字符串:
  String( char chars[ ] );
  String( char chars[ ], int startIndex, int numChars );
  String( byte ascii[ ], int hiByte );
  String( byte ascii[ ], int hiByte, int startIndex, int numChars );
  String使用示例:
  String s=new String() ; 生成一个空串

  下面用不同方法生成字符串"abc":
  char chars1[]={'a','b','c'};
  char chars2[]={'a','b','c','d','e'};
  String s1=new String(chars1);
  String s2=new String(chars2,0,3);
  byte ascii1[]={97,98,99};
  byte ascii2[]={97,98,99,100,101};
  String s3=new String(ascii1,0);
  String s4=new String(ascii2,0,0,3);

 (
3).用StringBuffer表示字符串

  StringBuffer( ); /*分配16个字符的缓冲区*/
  StringBuffer( int len ); /*分配len个字符的缓冲区*/
  StringBuffer( String s ); /*除了按照s的大小分配空间外,再分配16个
               字符的缓冲区*/

        2 、访问字符串

 

          (1).类String中提供了length( )、charAt( )、indexOf( )、lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。

  
◇ public int length() 此方法返回字符串的字符个数
  ◇ public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范围是0~length-1
  ◇ public int indexOf(int ch)
    public lastIndexOf(in ch)
  
  返回字符ch在字符串中出现的第一个和最后一个的位置
  ◇ public int indexOf(String str)
    public int lastIndexOf(String str)
  返回子串str中第一个字符在字符串中出现的第一个和最后一个的位置
  ◇ public int indexOf(int ch,int fromIndex)
    public lastIndexOf(in ch ,int fromIndex)
  返回字符ch在字符串中位置fromIndex以后出现的第一个和最后一个的位置
  ◇ public int indexOf(String str,int fromIndex)
    public int lastIndexOf(String str,int fromIndex)
  返回子串str中的第一个字符在字符串中位置fromIndex后出现的第一个和最后一个的位置。
  ◇ public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
   srcbegin 为要提取的第一个字符在源串中的位置, end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的

        字符串,dstbegin 为提取的字符串在目的串中的起始位置。
  ◇public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
  参数及用法同上,只是串中的字符均用8位表示。

 (2)
.类StringBuffer提供了 length( )、charAt( )、getChars( )、capacity()等方法。


  方法capacity()用来得到字符串缓冲区的容量,它与方法length()所返回的值通常是不同的。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值