Anda di halaman 1dari 4

Methods (Function):- set of instruction to perform a specific

task. It is reusable component of language.

Method Defintion:-

returntype methodname(parameters)
{
body
[return expression];
}

Method Calling :-

methodname(arguments);

Java is Completed Object Oriented Language. So all methods


should be within class block.

class Demo
{
void display(String text)
{
System.out.println(text);
}

public static void main(String[] k)


{
display("Hello");
display("Hello Again");
}
}

Method Overloading :- a class can contain more than one


method with same name but different parameters.

IN java 5, a new method is introduced which is called as


variable length argument method.

Now, we can define a method that can take multiple no of


arguments in a single paramter.

return-type methodname(datatype... parametername)


{

To accept multiple argument in a pramter, define paramter


will three ellipses after data type. Now it can take zero,
one or more than one arguments or even an array.
This paramter is act as an array internally.

A method can have only one variable length paramter.


Variable length paramter must be last parameter of a method.

------------------------------------------------------------

String data type :- non primitive data type. It is a predefine


Java class. is a collection of characters. String literals
are always declare with double quotes.

class String
{
....
....
....
}

String s = "hello";

String t = "welcome";

String variable are reference variable (that store address of


String value). String class provide many methods that are
use to perform operation on String literal.

length() :- return length of string literal.

String s = "hello";

int l = s.length(); // 5

--------------------------------------------------
charAt(index) :- return character at given index.

String s = "hello";
char a = s.charAt(2); // 'l'

---------------------------------------------------

toUpperCase() :- return String in upper case.

String s = "hello";

String t = s.toUpperCase(); // "HELLO"

----------------------------------------------------

toLowerCase() :- return String in lower case

String s = "HeLLo";

String t = s.toLowerCase(); // "hello"

------------------------------------------------------

indexOf(char) :- return index of a first match character.

String s = "Hello";

int i = s.indexOf('e'); // 1

------------------------------------------------------

lastIndexOf(char) :- return index of last match character.


String s = "Hello";

int i = s.lastIndexOf('l'); // 3

--------------------------------------------------------

contains(substring) :- check whether substring is contains


within a String literal, return boolean
value.

String s = "java is a programming language";

boolean t = s.contains("programming"); // true

boolean u = s.contains("scripting"); // false

-----------------------------------------------------------

equals(string) :- check whether our string is equal to another


string, return boolean value.

String s = "abcdef";

String t = "abcdef";

boolean x = s.equals(t); // true

boolean y = t.equals(s) // true

boolean z = s.equals("lmnop"); // false

------------------------------------------------------------

split(pattern) :- divide a string into words


(separated from pattern) and return array of String.

String s = "String is a java predefine class";

String[] t = s.split(" ");

// ["String","is","a","java","predefine","class"]

----------------------------------------------------------