Professional Documents
Culture Documents
Java 7 Basics
Java 7 Basics
============
Java plateform SE -7
major feature release of oracle
Features
===========
languages
-----------Project coin
Core
----concrrrancy, fork and join
collection updates
VM
--New Jvm instructions that simplify implementation of dynamic typed langu
ages
Garable First GC1 collector
------------------------------A server style GC, tageated for multiprocessor with large memori
es
i18n
-----support for unicode 6.0
new currencires types
nio-2
---new io API for java
nio-2 file system provides for zip/jar archives
ionet
-----SCTP (Stream control transmission protocol)
SDP(Socket direct protocol)
Use window vista IPV6 stack
security
--------a new native provider has been added that provides
several ECC based algorithm
ECC :Elliptic curve crytography
JDBC 4.1
--------rowset 1.1
supprot for ARM
clinet
-------swing JLayout component
Mgt
--Enhance mBean
Web
------updated XML stack....
languages
-----------Project coin
======================
A set of small languages changes to simplify day to day
programming task
1. Binary integral litrals
2. number with _
3. String in switch
4. Type inference for generic creation
5. catching multiple exception types
6. improved rethrowing exceptions
7. Resourece management ARM (Try With Resources)
Ex:
int n=0b1000000000000;//getting in binary
System.out.println(n);
byte data=0b111111;//up to 8 digit
data=0b010000001;//cant convert int to byte
data=0b000000001111;//leading zoro ignored
data=1b0000001;//negative not by 1b
data=-0b000001100;//negative by -0b
long ldata=0b11111111111111111111111111L;
float f=0b001111111000;
number with _
----------------------How easy to read these
int n1=100000000000;
int n2=1000000000;
now
int k=_100000000;
int n1=1_00000_0;
int n2=1________000;
double b=1_0000_00.00_00;
String in switch
--------------------String sport="hockey";
switch(sport)
{
case "tennis":System.out.println("tennis");
break;
case "vollyball":System.out.println("vollyball");
break;
case "TT":System.out.println("TT");
break;
default:System.out.println("cricket");
}
What if
Object sport="hockey";
Now:
List<Integer> list=new ArrayList<>();
Compiler can infer the type.......
............
...........
try{
if("foo".equals("foo"))
throw new FirstException();
else
throw new SecondException();
}
catch(FirstException|SecondException e){
e.printStackTrace();
}
...........
.................
inside an main
................
try
{
Demo.rethrowException("first");
} catch (Exception e) {
System.out.println(e.getClass());
}
In java 1.6 it will not compile .........
Ex:
class OpenDoor implements AutoClosable{
@Override
public void close() throws IOException {
System.out.println("close method is called by JVM.....")
;
}
}
.............
........
try(OpenDoor d=new OpenDoor())
{
}
catch(Exception e){}
O/P:
close method is called by JVM.....
Ex:2
What if we have two classes......
Now
...........
...............
try(
OpenDoor d=new OpenDoor();
OpenWindow w=new OpenWindow();
)
{
}
catch(Exception e){}
Close() method for OpenWindow class is called first then Open Doorclass
What happened in case of constructor of OpenDoor and OpenWindow class ..?
..
------------------------------------------------------------------------
...............
................
3) class java.nio.file.Files
----------------------------have static methods to operate on files/dir and other types of f
iles
4) class java.nio.file.FileSystem
----------------------------Important thing is that replace File(upto java6) with Path(java7)
Path realPath=path.toRealPath(LinkOption.NOFOLLOW_LINKS);
System.out.println(realPath);
//C:\exp\java7\nio-2\files\foo.txt
analysis the output.............
//creating directory
Path dir=Paths.get("files/newDir");
Files.createDirectories(dir);
//moving an file to dir
//Files.move(source, target, options)
Files.move(source, dir.resolve(source.getFileName()), Standard
CopyOption.REPLACE_EXISTING);
{
System.out.println(line);
wr.append(line,0,line.length());//writing
wr.newLine();//will append new line
}
}
catch(IOException e)
{
e.printStackTrace();
}
Step 2:
Use MyFileVisitor in Files.walkFileTree( .)
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
System.out.println(exc.getMessage());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
System.out.println("visited: "+dir);
return FileVisitResult.CONTINUE;
}
}
public class WalkingTheDirectoryTree {
public static void main(String[] args) throws IOException {
Path fileDir=Paths.get("files2");
MyFileVisiter visitor=new MyFileVisiter();
Files.walkFileTree(fileDir, visitor);
}
}