Java Basic Programming( Important Stuff)

Here is a list of some important topics in basic java. As I am still learning it, this post will be upated many times.

1. Substring :

(a) Passing one parameter:

Syntax: str.substring( n )

It displays the rest of the string after nth character.

(b) Passing two parameters:

Syntax: str.substring( start , end )

It displays characters between the start and end index.

2. String comparison:

Syntax: str1.compareTo( str2 )

(a) It returns postive number is str1 > str2. For example : str1="mike", str2="hate", str1.compareTo( str2 ) returns 5 as 'h' comes five times early than 'm'. If first character matches then it checks for the next one.

(b) It returns negative number is str1 < str2. For example : str1="mike", str2="hate", str1.compareTo( str2 ) returns -5 as 'h' comes 'h' comes five times early than 'm'. If first character matches then it checks for the next one.

(c) It returns 0 if both string are equal (fully equal)

3. Regex in java:

Regex are used for pattern matching in strings.Example

reg="[?,. ]+"

defnies a regex. So if we want to split a string str where ever it has a '?' or a '.' or a space ' ' or ',' then we can write String tokens[]=str1.split(reg) which will split str string on these points. I am still reading more about it as it is a complex topic.

4. Checking for empty string:

Syntax: str.isEmpty()

Returns ture if string is empty else false.

Comments

Popular Posts