package org.kodejava.basic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TypeSpecificCollection {
public static void main(String[] args) {
// Using a Generic can enable us to create a type specific collection
// object. In the example below we create a Map whose key is an Integer
// a have the value of a String.
Map<Integer, String> grades = new HashMap<>();
grades.put(1, "A");
grades.put(2, "B");
grades.put(3, "C");
grades.put(4, "D");
grades.put(5, "E");
// A value obtained from type specific collection doesn't need to
// be cast, it knows the type returned.
String value = grades.get(1);
System.out.println("value = " + value);
// Creating a List that will contain a String only values.
List<String> dayNames = new ArrayList<>();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
// We also don't need to cast the retrieved value because it knows the
// returned type object.
String firstDay = dayNames.get(0);
System.out.println("firstDay = " + firstDay);
}
}
Category Archives: Basic
Java Programming Keywords Summary
Here are the summary of the available keywords in the Java programming language. Keywords are reserved words that already taken and internally used by Java, so we cannot create variables and name it using this keyword.
| Keyword | Meaning |
|---|---|
abstract |
an abstract class or method |
assert |
used to locate internal program errors |
boolean |
the Boolean type |
break |
breaks out of a switch or loop |
byte |
the 8-bit integer type |
case |
a case of a switch |
catch |
the clause of a try block catching an exception |
char |
the Unicode character type |
class |
defines a class type |
const |
not used |
continue |
continues at the end of a loop |
default |
the default clause of a switch |
do |
the top of a do/while loop |
double |
the double-precision floating-number type |
| Keyword | Meaning |
|---|---|
else |
the else clause of an if statement |
enum |
define an enum type |
extends |
defines the parent class of a class |
final |
a constant, or a class or method that cannot be overridden |
finally |
the part of a try block that is always executed |
float |
the single-precision floating-point type |
for |
a loop type |
goto |
not used |
if |
a conditional statement |
implements |
defines the interface(s) that a class implements |
import |
imports a package |
instanceof |
tests if an object is an instance of a class |
int |
the 32-bit integer type |
interface |
an abstract type with methods that a class can implement |
long |
the 64-bit long integer type |
| Keyword | Meaning |
|---|---|
native |
a method implemented by the host system |
new |
allocates a new object or array |
null |
a null reference |
package |
a package of classes |
private |
a feature that is accessible only by methods of this class |
protected |
a feature that is accessible only by methods of this class, its children, and other classes in the same package |
public |
a feature that is accessible by methods of all classes |
return |
returns from a method |
short |
the 16-bit integer type |
static |
a feature that is unique to its class, not to objects of its class |
strictfp |
Use strict rules for floating-point computations |
super |
the superclass object or constructor |
switch |
a selection statement |
synchronized |
a method or code block that is atomic to a thread |
this |
the implicit argument of a method, or a constructor of this class |
| Keyword | Meaning |
|---|---|
throw |
throws an exception |
throws |
the exceptions that a method can throw |
transient |
marks data that should not be persistent |
try |
a block of code that traps exceptions |
void |
denotes a method that returns no value |
volatile |
ensures that a field is coherently accessed by multiple threads |
while |
a while loop type |
How do I compare string regardless of their case?
Here is an example of comparing two strings for equality without considering their case sensitivity. To do this we can use equalsIgnoreCase() method of the String class. Let’s see an example below:
package org.kodejava.basic;
public class EqualsIgnoreCase {
public static void main(String[] args) {
String uppercase = "ABCDEFGHI";
String mixed = "aBCdEFghI";
// To compare two string equality regarding it case use the
// String.equalsIgnoreCase method.
if (uppercase.equalsIgnoreCase(mixed)) {
System.out.println("Uppercase and Mixed equals.");
}
}
}
What’s needed to be prepared for learning Java programming?
At the time you decided to start to learn Java Programming you can start by downloading the Java Development Kit (JDK) from Java Download website. There are three different types of JDK, the Java SE (Java Standard Edition), Java EE (Java Enterprise Edition), Java ME (Java Mobile Edition).
From the website you can also download the Java API documentations which will sure be your first companion when learning the language. It is better also to download the Java Tutorial Series that was written by the Java experts.
In the tutorial you can learn from the basic of Java programming, introduction of the fundamental of Object-Oriented Programming (OOP) which is Java all about. Next you can also find trails in each subject of the API (Application Programming Interface) provided by Java library, such as the core package, how to communicate with a database, Java GUI programming, image manipulation, RMI, Java Beans Framework, etc.
When you want to write a code, you might wonder what editor or IDE you will need to use to start learning. A good text editor that supports a coloring will be a good candidate, colorful screen is better than just a black and white, isn’t it?
There are a lot of good text editor available today such as the VIM, NotePad++, TextPad, Editplus, UltraEdit. If you already have your preferred editor, you can use it of course.
If you’re ready for the big stuff, a bigger homework project, you might consider using an IDE (Integrated Development Environment) as you’ll be working with lots of Java classes, configuration files and build script for examples. There are many great IDE on the Java world from the free to the commercial product.
What IDE to use is really a developer decision, use whatever tools that can help you to improve your learning and programming activities. You can find IDE such as NetBeans, Eclipse, IntelliJ IDEA, etc.
Beside learning from the Java tutorials there are also many forums on the internet where you can discuss your doubts or your problems. Forums like JavaRanch, Stack Overflow are great forums with Java gurus that can help you to clarify your doubts and help you to solve your problem. Remember one thing when you ask for help, be polite, elaborate your problem clearly.
Good Java books on your desktop are good resources to study Java, from good books you can learn the nuts and bolts of the Java programming languages. When you have all your arsenal you can get the best out of you in learning Java. Have fun!
How do I get the command line arguments passed to the program?
When we create a Java application we might want to pass a couple of parameters to our program. To get the parameters passed from the command line we can read it from the main(String[] args) method arguments.
To make a class executable we need to create a main() method with the following signatures:
public static void main(String[] args) {
}
This method takes an array of String as the parameter. This array is the parameters that we pass to the program in the command line.
package org.kodejava.basic;
public class ArgumentParsingExample {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + (i + 1) + " = " +
args[i]);
}
// If you want to check if the number of supplied parameters
// meet the program requirement you can check the size of
// the arguments array.
if (args.length < 3) {
System.out.println(
"You must call the program as follow:");
System.out.println(
"java org.kodejava.example.basic.ArgumentParsingExample arg1 arg2 arg3");
// Exit from the program with an error status, for
// instance we return -1 to indicate that this program
// exit abnormally
System.exit(-1);
}
System.out.println("Hello, Welcome!");
}
}
When we try to run the program without argument we will see the following message:
$ java org.kodejava.basic.ArgumentParsingExample
You must call the program as follow:
java org.kodejava.basic.ArgumentParsingExample arg1 arg2 arg3
And when we pass three arguments we get something like:
$ java org.kodejava.basic.ArgumentParsingExample param1 param2 param3
Argument 1 = param1
Argument 2 = param2
Argument 3 = param3
Hello, Welcome!
