Bài giảng Slide được biên soạn rất đầy đủ , phù hợp cho mọi người , những ai đã nắm chắc lập trình hướng đối tượng thì học java khá đơn giản . Tài liệu bao gồm : + Lịch sử Java và cách cài đặt + Lập trình hướng đối tượng trong Java + Tìm hiểu các hàm , các lệnh trong Java + Bài tập về java mọi chủ điểm + Thư viện + Cách làm web trong java + Một số dự án cơ bản trong java
JAVA CORE AGENDA • Overview and setup • Rule & syntax • Object and Classes • Data Types • Operator, Decision Making, Loop • OOP • Collections • Exception • Multithreading OVERVIEW AND SETUP JAVA – OVERVIEW • Java programming language was originally developed by Sun Microsystems • Java built to suit various types of platforms – For example: J2EE for Enterprise Applications, J2ME for Mobile Applications • The latest release of the Java Standard Edition is Java SE • Java is guaranteed to be Write Once, Run Anywhere JAVA – OVERVIEW • In Java, everything is an Object Java can be easily extended since it is based on the Object model • Java is Platform Independent – This byte code is run in the Virtual Machine (JVM) • Java is designed to be easy to learn If you understand the basic concept of OOP Java, it would be easy to master HOW DOES IT WORK? HOW TO SETUP? • Download and install – http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ads-2133151.html • Setting Up the Path for Windows • Assuming you have installed Java in c:\Program Files\java\jdk directory • Right-click on 'My Computer' and select 'Properties' • Click the 'Environment variables' button under the 'Advanced' tab • Now, alter the 'Path' variable so that it also contains the path to the Java executable Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin' HOW TO SETUP? • Open command line and enter: java –version HOW TO SETUP? • Setup path JAVA_HOME FIRST JAVA PROGRAM • Create new file with path E:\training\javacore\helloword\Helloworld.java • Open file with notepath ++ or any java editor • Write this codes: public class H ellow orld { /* This is m y fi rst java program * This w illprint 'H ello W orld‘ as the output */ public static void m ain(String[] args) { System out.println("H ello, W orld"); // prints H ello W orld } } THE FINALLY BLOCK • The finally block follows a try block or a catch block A finally block of code always executes, irrespective of occurrence of an Exception • Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code USER-DEFINED EXCEPTIONS • You can create your own exceptions in Java Keep the following points in mind when writing your own exception classes − • All exceptions must be a child of Throwable • If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class • If you want to write a runtime exception, you need to extend the RuntimeException class LAB1 • Create test.txt with text as “Hello MSITA” • Write application with feature read file test.txt • Handle all exception and print text on file to console LAB2 • Create array menu with element String • User enter the number and print menu follow user enter • Create Lab2Exception and main function will throws this • Handle unchecked exception if user try to read data element>5 and It not crash application LAB3 • Create new BankException class • Extends Exception • Withdraw function will throws BankException • Try to handle this at main class MULTITHREADING WHAT IS MULTITHREADING? • Java is a multi-threaded programming language which means we can develop multi-threaded program using Java • A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs • Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program LIFE CYCLE OF A THREAD LIFE CYCLE OF A THREAD • New − A new thread begins its life cycle in the new state It remains in this state until the program starts the thread It is also referred to as a born thread • Runnable − After a newly born thread is started, the thread becomes runnable A thread in this state is considered to be executing its task • Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing LIFE CYCLE OF A THREAD • Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs • Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates THREAD PRIORITIES • Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled • Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10) By default, every thread is given priority NORM_PRIORITY (a constant of 5) CREATE A THREAD • If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface • You need to implement a run() method provided by a Runnable interface CREATE A THREAD • Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. LAB STEP • Create counter thread to print number count per second • The count number is auto increase 100 / second LAB STEP • Create main class and init counter