1.
What is the main
difference between Java platform and other platforms?
The Java platform differs from most other platforms
in that it's a software-only platform that runs on top of other hardware-based
platforms.
The Java platform has three elements:
The Java platform has three elements:
1. Java
programming language
2. The
Java Virtual Machine (Java VM)
3. The
Java Application Programming Interface (Java API)
2.
Explain ten following string handling
function with syntax.
1. String
|
The most direct way to create a
string is to write:
String greeting = "Hello
world!";
Whenever it encounters a string
literal in your code, the compiler creates a String object with its value in
this case, "Hello world!'.
ublic class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
|
2. String Length: |
This function returns the number of character in the
string.
public class StringDemo {
public static void main(String args[]) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
System.out.println( "String Length is : " + len );
}
}
|
3. Concatenating Strings: |
The String class includes a method for concatenating two
strings:
string1.concat(string2);
This returns a new string that is
string1 with string2 added to it at the end. You can also use the concat()
method with string literals, as in:
"My name is ".concat("Zara");
Strings are more commonly concatenated
with the + operator, as in:
"Hello," + " world" + "!"
which results in:
"Hello, world!"
Let us look at the following
example:
public class StringDemo {
public static void main(String args[]) {
String string1 = "know how ";
System.out.println("Don’t " + string1 + "I am?");
}
}
|
4. Java - String charAt() Method |
This method returns the character
located at the String's specified index. The string indexes start from zero.
Here is the syntax of this
method:
public char charAt(int index)
·
index -- Index of the character to be returned.
·
This method Returns a char at the specified index.
Example:public class Test {
public static void main(String args[]) {
String s = "Strings are immutable";
char result = s.charAt(8);
System.out.println(result);
}
}
This produces the following
result:
a
|
5. Java - String compareTo() Method |
There are two variants of this
method. First method compares this String to another Object and second method
compares two strings lexicographically.
Here is the syntax of this
method:
int compareTo(Object o)
or
int compareTo(String anotherString)
Here is the detail of parameters:
·
o -- the Object to be compared.
·
anotherString -- the String to be compared.
·
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.
public class Test {
public static void main(String args[]) {
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);
result = str2.compareTo( str3 );
System.out.println(result);
result = str3.compareTo( str1 );
System.out.println(result);
}
}
This produces the following
result:
0
10
-10
|
6. Java - String equals() Method |
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.
Here is the syntax of this
method:
public boolean equals(Object anObject)
Here is the detail of parameters:
·
anObject -- the object to compare this String
against.
·
This method returns true if the String are equal;
false otherwise.
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 produces the following
result:
Returned Value = true
Returned Value = true
|
7. Java - String toUpperCase() Method |
This method has two variants.
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()).
Second variant takes locale as an
argument to be used while converting into upper case.
Here is the syntax of this
method:
public String toUpperCase()
or
public String toUpperCase(Locale locale)
Here is the detail of parameters:
·
It returns the String, converted to uppercase.
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() );
}
}
This produces the following
result:
Return Value: WELCOME TO TUTORIALSPOINT.COM
|
8. Java - String toLowerCase() Method |
This method has two variants.
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()).
Second variant takes locale as an
argument to be used while converting into lower case.
Here is the syntax of this
method:
public String toLowerCase()
or
public String toLowerCase(Locale locale)
Here is the detail of parameters:
·
It returns the String, converted to lowercase.
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 produces the following
result:
Return Value :welcome to tutorialspoint.com
|
9. Java - String toCharArray() Method |
This method converts this string
to a new character array.
Here is the syntax of this
method:
public char[] toCharArray()
Return Value:
·
It returns a newly allocated character array, whose
length is the length of this string and whose contents are initialized to
contain the character sequence represented by this string.
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.toCharArray() );
}
}
This produces the following
result:
Return Value :Welcome to Tutorialspoint.com
|
10. Java - String replace() Method |
This method returns a new string
resulting from replacing all occurrences of oldChar in this string with
newChar.
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 produces the following
result:
Return Value :WelcTme tT TutTrialspTint.cTm
Return Value :WeDcome to TutoriaDspoint.com
|
3.
What are the different identifier states of
a Thread?
The different identifiers of a Thread are: R - Running or
runnable thread, S - Suspended thread, CW - Thread waiting on a condition
variable, MW - Thread waiting on a monitor lock, MS - Thread suspended waiting
on a monitor lock
4.
What is wrapper class Java?
Java is an object-oriented language and can view
everything as an object. A simple file can be treated as an object (with java.io.File),
an address of a system can be seen as an object (with java.util.URL),
an image can be treated as an object (with java.awt.Image) and
a simple data type can be converted into an object (with wrapper
classes). This tutorial discusses wrapper classes. Wrapper
classes are used to convert any data type into an object.
The primitive data types are not objects; they do
not belong to any class; they are defined in the language itself. Sometimes, it
is required to convert data types into objects in Java language. For example,
upto JDK1.4, the data structures accept only objects to store. A data type is
to be converted into an object and then added to a Stack or Vector etc. For
this conversion, the designers introduced wrapper classes.
Def:
As the name says, a wrapper class wraps (encloses) around a
data type and gives it an object appearance. Wherever, the data type is
required as an object, this object can be used. Wrapper classes include methods
to unwrap the object and give back the data type. It can be compared with a
chocolate. The manufacturer wraps the chocolate with some foil or paper to
prevent from pollution. The user takes the chocolate, removes and throws the wrapper
and eats it.
No comments:
Post a Comment