Its a difficult answer if you come from a Pascal (Delphi), C or C++ back ground where you had to initialize the array first with default values like 0 before using.
Java automatically initialize all elements in an array to default values, object references become null and primitive integers become 0.
This is in my SCJP exam studies, tricky since I come from a Pascal and C++ back ground.
In C/C++ you get some random data (whatever is in memory at that pointer address). So for them you need to initialize the array before use.
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Tuesday, February 23, 2010
Monday, February 22, 2010
Java Quiz: Array Initialization
Is the following correct? What will the output be?
And PS: Try to figure it out before actually running the code :-)
public class BirthDays{static int [] year = new int[100];public static void main(String [] args){for(int i=0;i<100;i++)System.out.println("year[" + i + "] = " + year[i]);}}
Please give your answer in the comments.
Wednesday, November 11, 2009
About SCJP Sun Certified Programmer For Java Exam
Questions from colleagues and readers of my blog, here is more information about the SCJP exam.
The SCJP exam is a theoretical exam with very tricky questions, it test your knowledge of the Java programming language which is the foundation of all future certifications like Java Developer, J2ME etc.
To study for the exam I suggest buying the SCJP Sun Certified Programmer For Java 6 Study Guide (R409.00) book and spend about a year studying, do mock exams etc until your sure you can pass the exam - its very difficult!
For help you can visit JavaRanch
Which is a website started by the authors of the SCJP book.
Phone Prometric Testing Centre on 0800 99 11 20 two weeks in advance, before writing the exams to book. Pricing can be checked with them.
The SCJP exam is a theoretical exam with very tricky questions, it test your knowledge of the Java programming language which is the foundation of all future certifications like Java Developer, J2ME etc.
To study for the exam I suggest buying the SCJP Sun Certified Programmer For Java 6 Study Guide (R409.00) book and spend about a year studying, do mock exams etc until your sure you can pass the exam - its very difficult!
For help you can visit JavaRanch
Which is a website started by the authors of the SCJP book.
Phone Prometric Testing Centre on 0800 99 11 20 two weeks in advance, before writing the exams to book. Pricing can be checked with them.
Thursday, June 25, 2009
Weekly Source Code: LINQ for Java Part 2
I hope everybody had a nice week, even if the winter is upon us - freezing cold with rain etc. Time to get the blankets / heaters out.
Last week I introduced JoSQL, this week I continue to show a sample JoSQL query... we start off with a keep it simple (KISS) sample with some points to remember.
Sample Query:
The goal of this JoSQL query is to get a list of contracts that all have a given product:
List contracts = .... // here we get a list of contracts
Query q = new Query();
q.parse("select * from com.sales.Contract where productId = :p");
q.setVariable("p", 5);
QueryResults qr = q.execute(contracts);
List contractResult = qr.getResults ();
Last week I introduced JoSQL, this week I continue to show a sample JoSQL query... we start off with a keep it simple (KISS) sample with some points to remember.
Sample Query:
The goal of this JoSQL query is to get a list of contracts that all have a given product:
List
Query q = new Query();
q.parse("select * from com.sales.Contract where productId = :p");
q.setVariable("p", 5);
QueryResults qr = q.execute(contracts);
List
- All JoSQL classes are kept in the org.josql package, Eclipse IDE will import these nicely for you.
- We start by creating a Query object instance this is where our query starts.
- q.parse will check your query for any issues, an exception will occure if the query is incorrect. Note that we need to provide the full package location of our Contract class else JoSQL wouldn't find the class. This can cause a problem if you refactor your code, like move the Contract class to a different package location but don't update the JoSQL query.
- The following line we set the :p variable using setVariable method.
- We then execute the query and put the results into qr which is an instance of QueryResults.
- contractResult will contain the result.
Friday, June 19, 2009
Weekly Source Code: LINQ for Java
Its been a long couple of weeks due to my current work load, working average 12 hours+ per day isn't fun. So I finally got some free time over a lunch break to continue with my weekly source code.
At work Java is our main programming language but its not so advance like C# (not to start a language war here!) one of them are LINQ but LINQ is also available in Java!
At work Java is our main programming language but its not so advance like C# (not to start a language war here!) one of them are LINQ but LINQ is also available in Java!
What Is LINQ?
Language Integrated Query (LINQ) was added to C# 3.0 / VB.Net 9.0 in MS Visual Studio.Net 2008. Its a query language part of the underline language for easy query of information like a database, online resources like a webservice or in our case collections (list, arrays, hashmaps etc). Different LINQ providers existing for collections, databases, Amazon, Facebook, Twitter, LDAP etc. Visit Wikipedia for more about LINQ.
Let's Meet: JoSQL
JoSQL is LINQ for Java that allow you to easily query collections (lists, arrays etc) using a SQL line syntax.
References
Disadvantages:
- No compile time checking since the LINQ code will only be evaluated when query.parse() method get called at run-time.
- You need to indicate the full package + class name in your query
- If you wish to use "NOT IN" or "IN" in the WHERE clause then you need to override the toString() method of the class since it needs to know which field to use to do the comparison with.
I will write more JoSQL samples in the next weekly source code. Also my solution to the toString() problem.
Tuesday, January 27, 2009
Weekly Source Code: Strings Are Immutable
Happy New Year 2009! :)
The String type that is actually a class in both Java and C# are immutable meaning that once you create it, it can't get changed and manipulating it will create a new string object.
This is import for when you want to populate a string in a loop (like a for loop) since everytime it will create a new string that have a performance impact on your code.
For example:
String val = null;
for (int i = 0; i < 100; i++)
{
val += i;
val += ", ";
}
The code above will create a new string object everytime for 100 times, you might not see or notice the impact but it does have a performance impact (test it if you wish).
Its better to use a string builder/buffer class and then append the string values to it since the object is only created once.
For example:
StringBuilder val = new StringBuilder();
for (int i = 0; i < 100; i++)
{
val.Append(i);
val.Append(",");
}
The above C# code use the string builder without performance impact.
Note that in C# its called the StringBuilder class where in Java its the StringBuffer class both is similar in functionality.
The String type that is actually a class in both Java and C# are immutable meaning that once you create it, it can't get changed and manipulating it will create a new string object.
This is import for when you want to populate a string in a loop (like a for loop) since everytime it will create a new string that have a performance impact on your code.
For example:
String val = null;
for (int i = 0; i < 100; i++)
{
val += i;
val += ", ";
}
The code above will create a new string object everytime for 100 times, you might not see or notice the impact but it does have a performance impact (test it if you wish).
Its better to use a string builder/buffer class and then append the string values to it since the object is only created once.
For example:
StringBuilder val = new StringBuilder();
for (int i = 0; i < 100; i++)
{
val.Append(i);
val.Append(",");
}
The above C# code use the string builder without performance impact.
Note that in C# its called the StringBuilder class where in Java its the StringBuffer class both is similar in functionality.
Monday, December 1, 2008
Weekly Source Code: Get Cents
Part of making changes to the Java currency to words code I pass in a double value parameter to the convert method (not a long) and add support for cents e.g. : $5.20 to equal "Five USD and Twenty cents".
An interesting problem I had was how do I get the cents part? e.g. $5.20 to get 20.
Firstly I had to format the number to round off to the last 2 digits so something like $5.2555 will not work. To do this in Java you can use the DecimalFormat class.
To get the cents I wrote:
double cents = new Double(new Double(number).doubleValue() - new Double(number).intValue()).doubleValue();
int iCents = (int) Math.round(cents * 100d);
The first line of code take the double value and subtract the integer value e.g. 5.20 - 5.0 = 0.20
The second line format the remaining value 0.20 to be an integer e.g. 20
Like always there are many ways in programming how this could be done and yes the above code works 100% since I wrote a very detail Unit test.
An interesting problem I had was how do I get the cents part? e.g. $5.20 to get 20.
To get the cents I wrote:
double cents = new Double(new Double(number).doubleValue() - new Double(number).intValue()).
int iCents = (int) Math.round(cents * 100d);
The second line format the remaining value 0.20 to be an integer e.g. 20
Like always there are many ways in programming how this could be done and yes the above code works 100% since I wrote a very detail Unit test.
Tuesday, October 21, 2008
C# From a Java Developer's Perspective
When I was moving from years of experience in Java to my first C# project with no knowledge of the framework or even the language I found this artical. It was very helpful to make the conversion from Java to the C# language especially since there's some differences that can bite you, when moving between them (like what I currently do on a daily basis).
Friday, October 3, 2008
Java Gotcha: mkdir vs mkdirs
In Java's File class there is the mkdir and mkdirs methods but note that both of them work very differently.
This is one of those Java Gotchas that can waste alot of debugging time.
- mkdir: Creates the directory named by this abstract pathname.
- mkdirs: Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories
This is one of those Java Gotchas that can waste alot of debugging time.
Saturday, September 20, 2008
TIOBE Software - Java TOP 1!
On the TIOBE Software list for the popularity of programming languages for September 2008 Java is TOP 1 on the list. :-)
Go Go Java!!
Go Go Java!!
Friday, September 19, 2008
Java URL vs URI
Goodday :)
You can get URL and a URI but remember that both of them work differently.
The URL encode the string where the URI is the actual resource.
I had a problem in a Java unit test (using jUnit) for PDFTable where my unit test failed since it was unable to load the fonts. The reason for this is that it was using the URL so trying to find the font files under an encoding path.
So the actual code fix is:
URI url = getClass().getClassLoader().getResource(fontResourceDir).toURI();
String path = url.getPath();
The toURI() method represents this abstract pathname.
You can get URL and a URI but remember that both of them work differently.
The URL encode the string where the URI is the actual resource.
I had a problem in a Java unit test (using jUnit) for PDFTable where my unit test failed since it was unable to load the fonts. The reason for this is that it was using the URL so trying to find the font files under an encoding path.
So the actual code fix is:
URI url = getClass().getClassLoader().getResource(fontResourceDir).toURI();
String path = url.getPath();
The toURI() method represents this abstract pathname.
PDFTable Open Source Project
Our employer allow Evan Summers and myself to release the Java code we wrote to generate PDF statements and invoices as a opensource project - PDFTable which is under the Apache License, Version 2.0
Im planning to make some modifications to it soon.
Im planning to make some modifications to it soon.
Subscribe to:
Posts (Atom)