SCJP 6: Done!!

Hi to all, and sorry for post absence. Those last weeks i've been working in some projects and studying for SCJP exam that i re-scheduled to past saturday. Fortunately, i've passed the exam with 85%
woohoo . God helped me with the exam giving me the intelligence to remember JAVA programming rules about OOP, Threads, serialization, inner classes, and soo on. So i feel very well, taking easy those days (is something like a "vacation") and thinking about next certification. I have a dilemma: I don't know if study for:
- SCBCD - Sun Certified Business Component Developer (EJB) or
- SCDJWS - Sun Certified Developer for Java Web Services (web services)
Because i don't know anything or a little bit (in web services) but both technologies are very interesting. Actually i been working with RESTful web services and i like to complement that with EJB 3 but unfortunately current SCBCD is about EJB 2 so i haven't take my desicion yet. But doesn't matter, i wanna rest some days (5 maximum XD) and after that start to sdudying something.
Also i can say that Big Moose Salon Forums helped me a lot. In that forum i found a page with links to mock exams that are very useful to practice after study some topic, and the forum to search my doubts or publish them to receive an answer from most experimented forum members.
Also i going to share some key topics of SCJP in future posts, so keep reading us!!!
SCJP: getDisplayCountry() and getDisplayLanguage()

A brief JAVA SCJP section about the use of two functions of the Locale class. Locale class is important when you are working with multiple language support for dates, numbers and so on, because Locale enables you to define a specific locale language and country for your format objects (like SimpleDateFormat or DecimalFormat) .
Let's say, that you want to know the name of a country in your language, for example in hindu. To do that we have a function called getDisplayCountry(), that gives you the name of the Locale's object specified country according to your JVM language configuration, or specify a different locale that your JVM's. For example if the language in your JVM is italian and you do this:
Locale lcUSCountry = new Locale("" ,"US");
System.out.println("United States in italian language: " + lcUSCountry.getDisplayCountry());
//Gets the next output: United States in italian language: Stati Uniti
But, you can use overloaded version of getDisplayCountry() that receives a Locale object as parameter, so that version returns the name of a Locale's object specified country according to the Locale object passed as parameter. In this example, you can get United States in hindu language (considering that JVM language is alwahs italian):
SCJP: Using File object (Part 2) Create a File Finder
Well, continuing with the use of File object, now we are woing to apply some of the key functions to build a simple file finder that is cross plattform, probably more powerful than Windows File Finder XD . The requeriment to satisfy with the file finder are:
"Show all the coincidences of files that name matches with file name entered by user, looking on all the files and sub directories of the directory specified by user."
Let's start!!
Firs of all, create a class named, for example, FileFinder, with main method (it will be an application without GUI), and create a List property, in my case i'm going to call it coincidencesLst, like this:
import java.util.List;
import java.util.ArrayList;
public class FileFinder {
public FileFinder() {
coincidencesLst = new ArrayList();
}
public static void main(String[] args) {
//Magic will be here
}
private List coincidencesLst;
public List getCoincidencesLst() {
return coincidencesLst;
}
}
The logic of our application will be the next (in that order):
- Ask the user for the file name that he wants to find
- Ask the user for the directory where he wants to find the file
- Search the file in the directory specified
- If a file in the directory, is a file and matches with the name specified by user, add the file to coincidences list
- If a file is a directory, search inside all the files/directories inside it with the same logic explained above
- If the coincidences list has files, show to user the path where those files resides
To ask the user the data we use the class Console that let us to get data from user input like this:
SCJP: Using File object (Part 1)
Right now i'm reading a little bit about some key classes of java.io package because is one of the SCJP topics, and y want to share with you some concepts about using File objects and doing operations with them.
First of all, File is not an object used to read or write information in files, it's more complex than that, it 's purpose is provide us an abstract representation of a file or directory and do file/directory special stuff like renaming, creating, deleting files and/or directories. If you wanna write or read file contents you should use Readers or Writers classes that have special methods to read lines of files, write lines and so on, those classes uses File objects to do those things, but that will be discused on another post.
As i said before, you can do operations like renaming files, deleting files, creating files and so on. But before that, let's view how to initialize a simple File object. The simplest way to do that is the next:
File myFile = new File("nameOfMyFile.txt");
As you can see, you initialize it passing the name of the file. Notice that we haven't specified something like the directory where file resides or even don't know if the file already exists!! First: when you create a File object, you hadn't created any file...yet, and, you can create File objects to represent new files (that will be created) or to represent existing files. If you want to create a file, you use the function createNewFile(), that returns a boolean, when is true, the file was created, else the file wasn't created. You can get false if for example, if the file already exists.
File myFile = new File("nameOfMyFile.txt");
try {
myFile.createNewFile();
}catch(IOException ex){ex.printStackTrace();}
As you can see, this method can throw an IOException so we have to handle it or declare it in the method. The question here is: Where is created the file? Well, the base directory is the directory where resides the jar file or the .class file. If you want to check if an existing file exists, you simply use the method exists() XD , suppose this: In the .class directory exists a file named someFile.txt and you want to verify it's existence, so, you have to do this:
SCJP:About JVM Garbage Collector

Reading about memory management in SCJP study guide, i learn valious things about Java Virtual Machine and Garbage collector. As you know, one of the features of java is the automatic memory management to let us to forget to write code to manage memory consumption and memory release.
SCJP: Curious stuff about switch statement
Hi to all (after a loong time without post), in the last days i've been reading about the JAVA basis like statements, operators and loops, where in a beginning i tought "This will be easy
", but i realize that there are many things about basic operators, loops, statements, that i didn't know. This kind of things are covered in SCJP study guide.
An example of this is about switch statement. As you know, switch allows us to emulate a group of if statements used to evaluate if an expression is equal to some value of a group of values. The conditions are that you can only use numeric types like byte, short, int, also with JAVA 5 autoboxing feature you can use it's equivalents on wrapper classes and enums.
The syntax is the next:
switch(argument) {
case value1:
//Java code
[break;]
case valueN:
//Java code
[break;]
[default:
//Java code
[break;]
]
}
as you can see, you have the argument to evaluate (the variable) that is compared against the values in each case. Well, in your while statement the case and default are optional, so you can have something like this:
switch(1) {
}
But it looks unusable and is stupid. Also there is some important (some unknown for me until a couple of days) rules about it, some of them are:
SCJP: JAVA narrow casting on primitive types
Hi to all... again
. Well, finally i passed a SCJP study chapter that i consider a little bit difficult for the amount of concepts and rules to apply, i'm talking about all stuff relationed to OOP, access modifiers and so on. Now, i'm reviewing about topics that i consider easy but always there are some things that we don't know.
That's my case with this: What happens when you do a conversion (talking about JAVA primitive types) where the destiny variable has a type lower than original type? For example if you have the next:
public class NarrowConversion {
public static void main(String[] args)
double myDoubleVal = 43323566.834;
byte myByteVal = (byte)myDoubleVal;
System.out.println("The value of myByteVal is " + myByteVal);
}
}
Misleading stuff in SCJP exam questions
Right now i'm studying some topis from SCJP study guide and i take a little exam about that, where i obtained a result not very good, mainly because exam questions have some tricky things like put "trash" code into code snippets like classes that are unused on working code, or classes that aren't inherited never, things like that. Also some questions cover things that you probably haven't imagined, some of these things are (Don't laugh):
JAVA Static methods: Can’t override but redefine (SCJP)
Confused? Me too, well, not now, but a few minutes ago yes. I was reading the Sun Certified Programmer for Java 6 Study Guide (recommended, a good book to prepare to SCJP certification exam) a little bit about static methods because i know how to use it but i didn't now why static methods works as they works. Finally i'm understanding some key concepts but when i read this:
//Finally, remember that static methods can't be overridden! This doesn't mean they
//can't be redefined in a subclass, but redefining and overriding aren't the same thing.
//Let's take a look at an example of a redefined (remember, not overridden), static
//method:
class Animal {
static void doStuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void doStuff() { // it's a redefinition,
// not an override
System.out.print("d ");
}
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}
What the hell!! What's the meaning of redefine a method and override a method? Well, i already know that override a method is to, as it's name says, to override the method definition and implementation of a superclass in a subclass, but redefining a method?
SCJP: How to do JAVA methods with variable arguments?
Sometimes you have a method that receives 2 arguments, like this:
public int operateNumbers(int arg1, int arg2)
public int operateNumbers(int arg1, int arg2)
{
//Do something
return 0;
}
but in other situation you need to use
that method but passing it 3 parameters, or 8, and so on.
An alternative could be modify the method to receive a List or an array of the type of arguments that you need to pass, but for this you have to modify the code that calls your method instantiating an array/List and fill with the elements that you want to pass to the method, like this:
public int operateNumbers(int[] arguments)
{
//Do something
return 0;
}
//...
public static void main(String[] args)
{
int[] parameters = new int[4];
parameters[0] = 5;
parameters[1] = 4;
parameters[2] = 10;
parameters[3] = 9;
int total = this.operateNumbers(parameters);
}
But another elegant solution can be use var-args methods. This kind of methods is a topic covered on SCJP study guide.







