• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Explain

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the following:
1) try{
FileInputStream dis = new FileInputStream("Hello.txt"); //Are we not creating a file here?
}catch(FileNotFoundException fne) {
System.out.println("No Such File Found");
return -1
}
catch(IOException ioe){
}
finally{
System.out.println("Doing Finally");
}
return 0;
}
}
Ans is :
No Such File Found, Doing Finally , -1
1. I Think We Are Creating File At The Commented Line.
2. Why is the order of output not : No Such File Found, -1, Doing Finally.
(-1 is in returned first)
2). String s1 = new String("Hello");
String s2 = new String("there");
String s3 = new String();
s3 = s1+s2;

The above compiles fine and outputs Hello there. Can We Concatenate two STRING OBJECTS ?

3) Which is correct:
Vector v = new Vector();
v.add("99");
OR
Vector v = new Vector();
v.addElement("99");

4) DataInputStream d = new DataInputSream(System.in);

DataInputStream takes a parameter of low lenvel stream, but System.in is an example of high level stream, then why is the above correct ?

Regards
Vineet
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vineet,
-->1) I think you only create files with FileOutputStream.
The constructor throws the exception sun api )(watch constructor details).
The finally is called BEFORE execution is handled back to the caller (the return -1). There is nice sample code in: Mughal, Rasmussen Source CodeName of file: DivisionByZero3.java.
2)This is normal string concatenation. The binary operator + is internally overloaded for Strings. You cannot Do operator overloading for yourself in Java.
You can even do things like:
String str1 = "its now ";
int1 = 3;
String str2 = " o clock in Germany";
String str3 = str1 + int1 + str2;
---> String3: "its now 3 o clock in Germany"
int1 as operand is implictly converted to String.

3)
sun.api.documentation about: void vector.addElement(Object o):

Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.
This method is identical in functionality to the[boolean] add(Object) method (which is part of the List interface).
Maybee that add(Object) is introduced later in the ListInterface than Vector class was designed (just a guess).
4) Java api-documentation states as only Constructor of DataInputStream:
Constructor Summary
DataInputStream(InputStream in)
Creates a FilterInputStream and saves its argument, the input stream in, for later use.


 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Axel ! Your concepts seem to be crystal clear!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic