Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
jdk1.7 new features
Previous Home Next

There are a number of features in Java 7 that will please developers.

  1. Things such as strings in switch statements.
  2. multi-catch exception handling.
  3. try-with-resource statements.
  4. the new File System API,
  5. extensions of the JVM.
  6. support for dynamically-typed languages.
  7. the fork and join framework for task parallelism, and a few others will certainly be embraced by the community.
Language Enhancement

Underscore for Numeric Literals: In JDK1.7, you could insert underscore(s) '_' in between the digits in an numeric literals (integral and floating-point literals) to improve readability.

Example

int anInt = 0b10101000_0101;
double aDouble = 5.115_7265;
float aFloat = 6.14_15_92_65f;
Catching Multiple Exception Types

In JDK1.7, a single catch block can handle more than one exception types. For example, before JDK1.7, you need two catch blocks to catch two exception types although both perform identical task:

try {
......
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
} catch(SQLException ex) {
ex.printStackTrace();
}
In JDK 7, you could use one single catch block,
with exception types separated by '|'.
try {
......
} catch(ClassNotFoundException|SQLException ex) {
ex.printStackTrace();
The try-with-resources Statement

For example, before JDK 7, we need to use a finally block, to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly. The code is messy!

JDK 7 introduces a try-with-resources statement, which ensures that each of the resources in try(resources) is closed at the end of the statement. This results in cleaner codes.

import java.io.*;
// Copy from one file to another file character by character.
public class testJDK7
{
public static void main(String[] args)
{
try (BufferedReader in = new BufferedReader(new FileReader("in.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {
int charRead;
while ((charRead = in.read()) != -1) {
System.out.printf("%c ", (char)charRead);
out.write(charRead);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Type Inference for Generic Instance Creation

Example

import java.util.*;
public class JDK7GenericTest {
public static void main(String[] args) {
// before-JDK 7
List<String> l1 = new ArrayList<String>();
// JDK 7 supports limited type inference for generic instance creation
List<String> l2 = new ArrayList<>();
l1.add("one");
l1.add("Two");
l2.add("three");
l2.add("four");
for (String item: l1) {
System.out.println(item);
}
for (String item: l2) {
System.out.println(item);
}
}
}
Switch on String :

Before JDK 7, only integral types can be used as selector for switch-case statement. In JDK 7, you can use a String object as the selector.

Example

String day = "SAT";
switch (day) {
case "MON": System.out.println("Monday"); break;
case "SAT": System.out.println("Saturday"); break;
case "SUN": System.out.println("Sunday"); break;
default: System.out.println("wrong");
}

String.equals() method is used in comparison, which is case-sensitive. Java compiler can generate more efficient code than using nested if-then-else statement.

This feature is handy in handling options specified in command-line arguments, which are Strings. For example (slightly neater code than using nested if-then-else statement),

// This program accepts three command-line options
// -c : create
// -v : verbose
// -d : debug
// More than one options can be specified in any order.
Binary Literals with prefix "0b"

In JDK 7, you can express literal values in binary with prefix '0b' (or '0B') for integral types (byte, short, int and long), similar to C/C++ language. Before JDK 7, you can only use octal values (with prefix '0') or hexadecimal values (with prefix '0x' or '0X').

You are also permitted to use underscore (_) to break the digits to improve the readability but you must start and end with a digit, e.g.,

int number1 = 0b01010000101000101101000010100010;
int number2 = 0b0101_0000_1010_0010_1101_0000_1010_0010;
int number3 = 2_123_456; 
// break the digits with underscore

Example

/ Some 32-bit 
'int' literal values
int aInt1 = 0b0101_0000_1010_0010_1101_0000_1010_0010;
int bInt2 = 0b0011_1000;
// An 8-bit 'byte' literal value. By default, literal values are 'int'.
// Need to cast to 'byte'
byte aByte = (byte
)0b0110_1101;
// A 16-bit 'short' literal value
short aShort = (short)0b0111_0101_0000_0101;
// A 64-bit 'long' literal value. Long literals requires suffix "L".
long aLong = 0b1000_0101_0001_0110_1000_0101
_0000_1010_0010_1101_0100_0101_1010_0001_0100_0101L;
// Formatted output: "%d" for integer in decimal, "%x" in hexadecimal, "%o" in octal.
// Take note that "%b" prints true or false (for null), NOT binary.
System.out.printf("%d(%x)(%o)(%b)\n"
, anInt1, anInt1, anInt1, anInt1);
System.out.printf("%d(%x)(%o)(%b)\n"
, aByte, aByte, aByte, aByte);
}
}
Previous Home Next