You are on page 1of 10

Number Class Methods:

Integer- int
Double- double
Float- float
Long- long
Byte- byte
Short- short

1. Number class object to primitive data type:


int intValue()
Eg: Integer to int
Integer obj = new Integer(15);

// returns the value of this Integer as an int


int i = obj.intValue();

2. Compare Number class object with argument:


int compareTo(Integer anotherInteger)
Integer>argument=
Integer<argument=
Integer=argument=

1
-1
0

Integer x = 5;
System.out.println(x.compareTo(3)); //prints 1
System.out.println(x.compareTo(5));

//prints 0

System.out.println(x.compareTo(8)); // prints -1

3. To check equality:
boolean equals(Object o)
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;

System.out.println(x.equals(y));

//false

System.out.println(x.equals(z)); //true
System.out.println(x.equals(a)); //false

If objects are of same type and have same value, returns true
Else false.
4. valueOf int/string as object:
static Integer valueOf(int i)
static Integer valueOf(String s)
Integer x =Integer.valueOf(9); //9
Double c = Double.valueOf(5);//5.0
Float a = Float.valueOf("80");

//80.0

Integer b = Integer.valueOf("444"); //444

5. object or primitive data type to string


String toString()
static String toString(int i)
Integer x = 5;
System.out.println(x.toString());

//5 as a string

System.out.println(Integer.toString(12)); // 12 as a string, here argument is


primitive data type

6. convert from string to primitive data type:


static int parseInt(String s)
int x =Integer.parseInt("9"); //9
double c = Double.parseDouble("5"); //5.0

7. Other methods:
All returns primitive data type, has parameter of primitive data type
Math.sqrt(x), Math.pow(x,y), Math.min, Math.max(), Math.abs(),
Math.ceil(x), Math.floor(x), Math.sin(x), Math.asin(x)
Math.random()== random no between 0.0 to 1.0

Character Class Methods


1. Case conversion
char toUpperCase(char ch)
char toLowerCase(char ch)
System.out.println(Character.toUpperCase('c')); //C
System.out.println(Character.toLowerCase('C')); //c

2. String toString(char ch)

String

String greeting = "Hello world!";

Or
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);

1. Length=
greeting.length() = 12

//java does not have \0

2. Concate:
Add strings
Eg: string1= string1 + string2
Str= hel + lo
3. char charAt(int index)
s=hellogram;
eg: char ch= s.charAt(5)

//g

4. int compareTo(String anotherString)


int compareToIgnoreCase(String anotherString)
The value 0 if the argument is a string lexicographically equal to this
string; a value less than 0 if the argument is a string lexicographically
greater than this string; and a value greater than 0 if the argument is a
string lexicographically less than this string.
String str1 = "Strings are immutable";
String str2 = "Strings are immutable";
String str3 = "Integers are not immutable";

int result = str1.compareTo( str2 );


System.out.println(result); //0

result = str2.compareTo( str3 );


System.out.println(result); //10

result = str3.compareTo( str1 );


System.out.println(result); //-10

Str2 ka S> str3 ka I by 10 chars .


5. public boolean equalsIgnoreCase(String anotherString)
public boolean equals (String anotherString)
returns true if equal else false.
6. Get some characters from string and store them in array
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
String Str1 = new String("Welcome to Tutorialspoint.com");
char[] Str2 = new char[7];
Str1.getChars(2, 9, Str2, 0);

Str2 will contain {l,c,o,m,e, ,t}


7. Find index
public int indexOf(int ch )
public int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex)
String Str = new String("Welcome to Tutorialspoint.com");
String SubStr1 = new String("Tutorials");
String SubStr2 = new String("Sutorials");
System.out.println(Str.indexOf( 'o' )); //4
System.out.println(Str.indexOf( 'o', 5 )); //9
System.out.println( Str.indexOf( SubStr1 )); //11
System.out.println( Str.indexOf( SubStr1, 15 )); //-1
System.out.println(Str.indexOf( SubStr2 )); //-1

8. Split string with delimiter:


public String[] split(String regex, int limit)

String Str = new String("Welcome-to-Tutorialspoint.com");


for (String retval: Str.split("-", 2)){
System.out.println(retval);
}

Gives outout
Welcome
to-Tutorialspoint.com

9. Substring:
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
String Str = new String("Welcome to Tutorialspoint.com");
System.out.println(Str.substring(10) );// Tutorialspoint.com
System.out.println(Str.substring(10, 15) ); //Tuto

10. Form char array from string:


public char[] toCharArray()
String Str = new String("Welcome to Tutorialspoint.com");
Char [] ch= Str.toCharArray()

11. Case
public String toLowerCase()
public String toUpperCase()
Strig str2= Str.toLowerCase()

12.Convert from primitive to string:


static String valueOf(int i)
String str2= String.valueOf(100) //100

13.Passing string to method:


public class PassingStringToMethod {
public static void main(String[] args) {
String str = "Hello";
System.out.println("In main: Before Passing String " +
"to method: " + str);
method(str);
System.out.println("In main: After returning " +
"from method: " + str);
}
public static void method(String strTest) {
strTest += " World";
System.out.println("In method(): " + strTest);
}
}

14.Return string;
Public String method()
{
Str= hey;
Return str;
}

Array

double[] myList = new double[10];

length= myList.length
1. Passing array to function
public static void printArray(int[] array) {

for (int i = 0; i < array.length; i++) {


System.out.print(array[i] + " ");
}
}

2. Returning array from function


public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {


result[j] = list[i];
}
return result;
}

3. Sort array
public static void sort(int[] a)
public static void sort(int[] a, int fromIndex, int toIndex)

int iArr[] = {2, 1, 9, 6, 4};


Arrays.sort(iArr);
Arrays.sort(iArr, 0, 3); //includes index 0 and 3

4. Binary Search
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)

Before doing search, array needs to be sorted first.!!


int intArr[] = {30,20,5,12,55};
Arrays.sort(intArr);
int searchVal = 12;

int retVal = Arrays.binarySearch(intArr,searchVal)


int retVal = Arrays.binarySearch(intArr,1,3,searchVal);

returns index of searchVal= 1


5. Copy array into other with specific length.
public static int[] copyOf(int[] original,int newLength)
public static int[] copyOfRange(int[] original, int from, int to)
copies newLength elements from original to new array. If newLength > size,
pads zeroes at the end.
int[] arr1 = new int[] {45, 32, 75};
int[] arr2 = Arrays.copyOf(arr1, 5);

here arr2[3] and arr2[4] are 0;


6. Compare arrays:
public static boolean equals(int[] a, int[] a2)
int[] arr1 = new int[] { 10, 12, 5, 6 };
int[] arr2 = new int[] { 10, 12, 5, 6 };
int[] arr3 = new int[] { 10, 5, 6, 12 };
boolean retval=Arrays.equals(arr1, arr2);

//returns true

boolean retval2=Arrays.equals(arr2, arr3); //returns false

7. Array tostring
public static String toString(int[] a)
int[] i1 = new int[] { 33, 12, 98 };
System.out.println("The string representation of array is:");
System.out.println(Arrays.toString(i1));

Output:

The string representation of array is:


[33, 12, 98]

Arraylist
1.
2.
3.
4.
5.
6.
7.
8.
9.

void add(int index, Object element): add element at index


boolean add(Object o): add element at end
void clear(): remove all elements from list
boolean contains(Object o): true if o is in list
Object get(int index);
int indexOf(Object o)
Object remove(int index);
int size()
Object[] toArray()

ArrayList al = new ArrayList();


al.add(4);
al.remove();
al.size();

You might also like