Rekha Sivadasan

Greenhorn
+ Follow
since Feb 15, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Rekha Sivadasan

The output for the below given code is 3. It could have easily been 1 also. How is this the method choosen here?
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
}
Thanks
Rekha
In the programme below, please exaplain why the value of sup.index is 5 instead of 2.
class Super{
public int index = 5;

Super(){
System.out.println("The value of index " + index + " In Super Class");
}
public void printVal(){
System.out.println( "Super" );
System.out.println("value of index in super " + index);
}
}
class Sub extends Super
{
public int index = 2;

Sub(){
System.out.println("The value of index " + index + " In Sub Class");
}
public void printVal(){
System.out.println( "Sub" );
System.out.println("value of index in sub " + index);
}
}
public class Runner{
public static void main( String argv[] ){
Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
Thanks
Rekha
Hi Jini
The only thing you can have in a class and outside a method is static blocks. Thus the initialization will give complie time error.
Hope that helps.
Rekha
IO
There is a small mistake in the above programme. f.write(i) should be replaced by f.write(b[i]). The output is almost similar though.
I also tried using String class and getBytes method. This time the actual value gets stored in the lop.txt. The code is as follows -
import java.io.*;
public class lapore{
public void lapore() throws FileNotFoundException,IOException{
OutputStream f=new FileOutputStream("lop.txt",true);
byte b[]=new byte[123];
String s="";
int p=1;
for(int i=0;i<b.length;i++){ >
s+= ++p;
}
b= s.getBytes();
f.write(b);
f.close();
}
public static void main(String[]args)throws FileNotFoundException,IOException {
lapore lapore=new lapore();
lapore.lapore();
}
}

quoting API about the getBytes method.
public byte[] getBytes()
Convert this String into bytes according to the platform's default character encoding, storing the result into a new byte array.

Strange though.
IO
Here int p is being cast into a char and stored in the file. Check this out -
import java.io.*;
public class lapore{
public void lapore() throws FileNotFoundException,IOException{
OutputStream f=new FileOutputStream("lop.txt",true);
byte b[]=new byte[123];
int p=1;
for(int i=0;i<b.length;i++){ >
b[i] = (byte)++p;
System.out.print((char)p);
f.write(i);
}
f.close();
}
public static void main(String[]args)throws FileNotFoundException,IOException {
lapore lapore=new lapore();
lapore.lapore();
}
}
The output with System.out is same as the values being stored in lop.txt.
yep you are right Mafalda. They are in lowercase , while the last one is toDegrees() instead of toDegree().
Thanks
Rekha
ok cool. Thanks Hima for clearing the confusion.
Rekha
Sorry this is not a threads question as mentioned in the title. Its gc.
Which methods does java.lang.Math include for trigonometric computations?
a) sin()
b) cos()
c) tan()
d) aSin()
e) aCos()
f) aTan()
g) toDegree()
The answer for this is only a,b & c. Why aren't the rest considered to be trinometric functions? Trignometry is not my cup of tea. So please explain.
Rekha

Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
The correct answer given is a, b,c.
I consider only b to be correct. Can someone please throw some light on this.
Thanks
Rekha

You get a compile time error for the programme since you can't create an Integer object like that.
As far as String are concerned both the output will be true since Strings are immutable and when similar strings are created without the new keyword both a and a1 refer to the same object.
equals is a method defined in the Object class. It operates on objects and takes in an argument of Object and returns a boolean value. It has been declared as public boolean equals(Object o). This method has been overriden in various classes like String, Boolean, Integer etc.
In String class equals compares the characters inside String object, whereas == compares object references.
The same holds good for Integer class. equals method - where the actual int value is compared whereas == compares object references.
Check the following programme out. It will make things more clear.
class EqualsTest{
public static void main(String arg[]){
String a = new String("Test");
String a1 = new String("Test");
Integer i = new Integer(100);
Integer i1 = new Integer(100);
System.out.println("a.equals(a1) " + a.equals(a1));
System.out.println("i.equals(i1) " + i.equals(i1));
System.out.println(a == a1);
System.out.println(i == i1);

}
}
Hope this helps.
Rekha
The rule about Exception is that it has be be handled or declared and the overriding method can't throw any checked exception that are not declared for the overridden method. But why am I being forced to declare and handle the exception in the below code.
//Removing throws clause

class Overriding{
protected void test(String s){
System.out.println("In the Superclass Method test " + s);
}

public static void main(String arg[]){
Overriding superOver = new Overriding();
Override subOver = new Override();

try{
superOver.test("Hello super");
System.out.println();
System.out.println();
subOver.test("Hello sub");
}catch(IllegalAccessException e){
System.out.println(e);
}finally{
System.out.println("This is always execute");
}
}
}
class Override extends Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In method test of subclass " + s );
}
}
Removing the throws clause gives the following error message -
The method void test declared in the class Override cannot override the method of the same signature declared in the class Overriding. Their throws clause are incompatible
protected void test(String s) throws IllegalAccessException
//comment the catch block add the throws clause

class Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In the Superclass Method test " + s);
}

public static void main(String arg[]){
Overriding superOver = new Overriding();
Override subOver = new Override();

try{
superOver.test("Hello super");
System.out.println();
System.out.println();
subOver.test("Hello sub");
/*}catch(IllegalAccessException e){
System.out.println(e);*/
}finally{
System.out.println("This is always execute");
}
}
}
class Override extends Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In method test of subclass " + s );
}
}
Commenting the catch block gives the following error
Exception java.lang.IllegalaccessException must be caught or it must be declared in the throws clause of this method
superOver.test("Hellow super");

When both throws clause and catch block is given the programme give not error message.
In case of NullPointerException(uncheckedexception) we need to either handle or declare the exception. Why is that so?