Sunday, 5 February 2017

JAVA LIBRARY FUNCTIONS

Access Specifiers: In General, when a Sub-Class is created from a base Class, then all data members and method members of Parent (base) class are inherited in the child class and hence they can be accessed in child class because they are visible. But some time, it is required to restrict some methods from being inherited in child class to maintain the privacy and security. Java provides some Access Specifiers, which can control the visibility/access of the Parent (base) Class from Child (sub) class. The members (Data and Methods) of the Parent Class may be defined as Private, Public, Protected, and Default, which may limit its accessibility or visibility in the derived (child) classes.  Private : Members are accessible only inside their own class and no where else.  Protected: Members are accessible in own class, all classes within package and all Sub-classes in different package.  Public: Members are accessible everywhere in all the classes.  Package (default): Members without any specifier assumed package level scope i.e. accessible to all classes inside the package only. Abstract Class: An Abstract class simply represents a concept for its sub-classes. An Abstract class works as template for its sub-classes which contains only data members and method prototype i.e. methods without code (concept). Sometimes, we need to define a super-class having general characteristics (data) and behaviour (generic methods) for its sub-classes. A Sub-class may re-define the methods or overridden to perform a task since super-class contains only the prototype (method with empty body). Abstract classes are normally used as base class in inheritance for which no object is required e.g. JOptionPane Class in Java is Abstract class because it requires no object. Whereas JTextField, JLabel classes etc. are called Concrete classes because they requires an object like ‘jTextField1’ for using them. Role of Final keywords in inheritance: The final keyword can be used with- Variable, Methods and Class names. The effect of final keywords is as follows.  final variables works as constant i.e. the value of a final variables can’t be changed.  final methods can’t be overridden by sub- class.  final class can’t be extended. Concept of Polymorphism: In Simple term, Polymorphism means Multiple forms of behaviour. For example, a person may exhibit different behaviour in different places or situation. In Object Oriented Programming, a Method or Operator may exhibit different behaviour for different sets of input given. For example the ‘+’ operator in Java gives different result for different input (integer and string) 2+3 gives 5 but “Hello”+”Java” gives “HelloJava” Here, + operators exhibits different behaviour for numbers and string values i.e. + operator is overloaded. Same as Math.round() function exhibit different behaviour for float and double type values i.e. Method Overloading. Polymorphism is implemented as Method Overloading and Operator Overloading i.e. overloaded with different functionalities. Polymorphism makes your program code compact, smarter and faster.

Number Methods

Following is the list of the instance methods that all the subclasses of the Number class implements −
Sr.No. Method & Description
1 xxxValue() Converts the value of this Number object to the xxx data type and returns it.
2 compareTo() Compares this Number object to the argument.
3 equals() Determines whether this number object is equal to the argument.
4 valueOf() Returns an Integer object holding the value of the specified primitive.
5 toString() Returns a String object representing the value of a specified int or Integer.
6 parseInt() This method is used to get the primitive data type of a certain String.
7 abs() Returns the absolute value of the argument.
8 ceil() Returns the smallest integer that is greater than or equal to the argument. Returned as a double.
9 floor() Returns the largest integer that is less than or equal to the argument. Returned as a double.
10 rint() Returns the integer that is closest in value to the argument. Returned as a double.
11 round() Returns the closest long or int, as indicated by the method's return type to the argument.
16 pow() Returns the value of the first argument raised to the power of the second argument.
17 sqrt() Returns the square root of the argument.
27 random()
Returns a random number.

ROUND

Description

The method round returns the closest long or int, as given by the methods return type.

Syntax

This method has the following variants −
long round(double d)
int round(float f)

Parameters

Here is the detail of parameters −
  • d − A double or float primitive data type.
  • f − A float primitive data type.

Return Value

  • This method returns the closest long or int, as indicated by the method's return type, to the argument.

Example

public class Test { 

   public static void main(String args[]) {
      double d = 100.675;
      double e = 100.500;
      float f = 100;
      float g = 90f;

      System.out.println(Math.round(d));
      System.out.println(Math.round(e)); 
      System.out.println(Math.round(f)); 
      System.out.println(Math.round(g)); 
   }
}
This will produce the following result −

Output

101
101
100
90 
POW()

Description

The method returns the value of the first argument raised to the power of the second argument.

Syntax

double pow(double base, double exponent)

Parameters

Here is the detail of parameters −
  • base − Any primitive data type.
  • exponenet − Any primitive data type.

Return Value

  • This method returns the value of the first argument raised to the power of the second argument.

Example

public class Test { public static void main(String args[]) { double x = 11.635; double y = 2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y)); } } This will produce the following result −

Output

The value of e is 2.7183 pow(11.635, 2.760) is 874.008
 

Java –random() Method Description The method is used to generate a random number between 0.0 and 1.0. The range is: 0.0 =< Math.random < 1.0. Different ranges can be achieved by using arithmetic operations. Syntax static double random() Parameters Here is the detail of parameters:  This is a default method and accepts no parameter. Return Value  This method returns a double.

System.out.println( Math.random() ); System.out.println( Math.random() );

Description

This method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

Syntax

Here is the syntax of this method −
public String replace(char oldChar, char newChar)

Parameters

Here is the detail of parameters −
  • oldChar − the old character.
  • newChar − the new character.

Return Value

  • It returns a string derived from this string by replacing every occurrence of oldChar with newChar.

Example

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.replace('o', 'T'));

      System.out.print("Return Value :" );
      System.out.println(Str.replace('l', 'D'));
   }
}
This will produce the following result −

Output

Return Value :WelcTme tT TutTrialspTint.cTm
Return Value :WeDcome to TutoriaDspoint.com
equals()

Description

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Syntax

Here is the syntax of this method −
public boolean equals(Object anObject)

Parameters

Here is the detail of parameters −
  • anObject − the object to compare this String against.

Return Value

  • This method returns true if the String are equal; false otherwise.

Example

public class Test { public static void main(String args[]) { String Str1 = new String("This is really not immutable!!"); String Str2 = Str1; String Str3 = new String("This is really not immutable!!"); boolean retVal; retVal = Str1.equals( Str2 ); System.out.println("Returned Value = " + retVal ); retVal = Str1.equals( Str3 ); System.out.println("Returned Value = " + retVal ); } } This will produce the following result −

Output

Returned Value = true Returned Value = true

substring()

Description

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.

Syntax

Here is the syntax of this method −
public String substring(int beginIndex)

Parameters

Here is the detail of parameters −
  • beginIndex − the begin index, inclusive.

Return Value

  • The specified substring.

Example

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.substring(10) );

   }
}
This will produce the following result −

Output

Return Value : Tutorialspoint.com 
substring(begindex,endindex)

Description

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.

Syntax

Here is the syntax of this method −
public String substring(int beginIndex, int endIndex)

Parameters

Here is the detail of parameters −
  • beginIndex − the begin index, inclusive.
  • endIndex − the end index, exclusive.

Return Value

  • The specified substring.

Example

import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.substring(10, 15) ); } } This will produce the following result −

Output

Return Value : Tuto


.toString()
check net again

toLowerCase()

Description

This method has two variants. The first variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()).
The second variant takes locale as an argument to be used while converting into lower case.

Syntax

Here is the syntax of this method −
public String toLowerCase()

Parameters

Here is the detail of parameters −
  • NA

Return Value

  • It returns the String, converted to lowercase.

Example

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :");
      System.out.println(Str.toLowerCase());
   }
}
This will produce the following result −

Output

Return Value :welcome to tutorialspoint.com 
 
.toUpperCase(0

Description

This method has two variants. The first variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()).
The second variant takes locale as an argument to be used while converting into upper case.

Syntax

Here is the syntax of this method −
public String toUpperCase()

Parameters

Here is the detail of parameters −
  • NA

Return Value

  • It returns the String, converted to uppercase.

Example

import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.toUpperCase() ); } }

Output

Return Value :WELCOME TO TUTORIALSPOINT.COM

 


 

 

No comments:

Post a Comment