Java Questions with Answers #6

1) Explain the following method of Character class with example.

Consider for following question
char ch1,ch2,ch3;
ch1=’1’;
ch2=’a’;
ch3=’D’

(1).isDigit
It returns true if the parameter passed to it is digit otherwise returns false.
Example – System.out.println(ch1.isDigit());
true

(2).isLetter
It returns true if the parameter passed to it is letter otherwise returns false.
Example – System.out.println(ch2.isLetter());
true

(3).isUpperCase
It returns true if the parameter passed to it is a upper case letter otherwise returns false.
Example – System.out.println(ch2.isUpperCase());
False

(4).isLowerCase
It returns true if the parameter passed to it is a lower case letter otherwise returns false.
Example - System.out.println(ch3.isLowerCase());
False

2) What is StringBuffer? What is difference between String and StringBuffer?

The StringBuffer is a class which is alternative to the String class. But StringBuffer class is more flexible to use than the String class. That means, using StringBuffer we can insert some components to the existing string or modify the existing string but in case of String class once the string is defined then it remains fixed. The StringBuffer and the StringBuffer are almost one and the same. The StringBuffer or StringBuilder have three constructors and 30 methods.

3) Explain various methods of StringBuffer class.

(1).append
This method appends the string to the Buffer.
Syntax:- strbuf.append(String str);

(2).charAt
It returns a specific character from the sequence which is specified by the index.
Syntax :- ch=strbuf.charAt(index);

(3).capacity
It returns the capacity of the string buffer. It is 16 character more than its length.
Syntax :- int c=strbuf.capacity();

(4).delete
It deletes the characters from the string specified by the starting and ending index.
Syntax :- strbuf.delete(int stindex,int endindex);

(5).insert
It inserts the character at the position specified by the offset.
Syntax :- strbuf.insert(int offset,char ch);

(6).length
It returns the length of the string buffer.
Syntax :- int len=strbuf.length();

(7).setCharAt
The character specified by the index from the string buffer is set to ch.
Syntax :- strbuf.setCharAt(int index,char ch);

(8).replace
It replaces the characters specified by the new string.
Syntax :- strbuf.replace(int start,int end,String str);

(9).reverse
Syntax :- strbuf.reverse();

(10).setLength
It sets the length of the string buffer.
Syntax :- strbuf.setLength(int new_len);

4) What is absolute filename and relative filename?

The file name can be specified with its complete path and drive letter. Such a specification of file name is called absolute filename. For example – c:\test\myprogram.html where test is a current directory in which myprogram.html
lies.

The file name is a file name relative to the current directory. That means while specifying the relative file name we should not mention the complete path of the corresponding file. For example – new File(“myprogram.html”);

5) What is this reference?

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the thiskeyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the
method was invoked. You can use this anywhere a reference to an object of the current class' type is permitted.

To better understand what this refers to, consider the following version of Box():
//A redundant use of this.
Box(double w, double h, double d)
{
this.width=w;
this.height=h;
this.depth = d;
}
The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking object. While it is redundant in this case, this is useful in other contexts, one of which is explained in the next section.

6) What is difference between Methods and Constructor?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.