Introduction to java


Published on

Introduction to java is first tutorial which will give basic idea of java programming. its nice tutorial to learn java step by step. Please leave your comments.

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to java

  1. 1. Say Hello to Java 1 Rakesh Kumar Singh
  2. 2. Say Hello to Java 2 Introduction Java Technology Structure Installation instruction of JDK 5.0 JDK directory structure Bin Lib JRE, etc Steps to Develop Java Program Composing Compilation Running Developing Simple Java Program Case 1: From bin folder Case 2: Outside of bin folder Case 3: .java and .class files in separate location Questions
  3. 3. Say Hello to Java 3 Introduction To develop any java program we require software, it’s called J2SDK (Java 2 Standard Development kit) Or J2SEDK (Java 2 Standard Edition Development Kit) Or, Most Commonly its known as JDK (Java Development Kit) Now, the main component of JDK is J2SRE (Java 2 Standard Run Time Environment) Or J2SERE (Java 2 Standard Edition Run Time Environment) Or, most commonly called JRE (Java Run Time Environment)
  4. 4. Say Hello to Java 4 Java Technology Structure J2SE is base for all java technology. J2EE and J2ME both are independent to each other but Base of both streams (EE & ME) is J2SE. J2SE J2EE Java 2 Enterprise Edition J2ME Java 2 Micro/Mobile Edition
  5. 5. Say Hello to Java 5 Installation instruction of JDK 5.0 Here we have to perform following task  Change the location of installation from OS drive (Generally C :) to any other drive.  Don’t use space in any of folder or directory name  Here we will see following Components o Development Tools [Mandatory] o Source [can be filter but don’t do it] o Demo [can be filter but don’t do it] o Public JRE [Filter this]  Filter Public JRE(Don’t Install)  Public JRE is used for production purpose so while development we don’t require this.  All these components will store or install at same location, we can’t install different component in different location.  We can’t filter Development tools while installing JDK. Except this we can filter all components. Download JDK 5.0 JDK 5.0 .exe Follow the instruction (Accept License) Custom setup Install Finish By default java will install in OS drive. That we should change to other location. Spaces in folder or dir name can misguide to OS and other support tools, so it’s not advisable to use.
  6. 6. Say Hello to Java 6 Installation Step by step: JDK 5.0 exe Double click on exe file Processing screen will appear It will take few minute to process. After processing, License Screen will appear where we have to accept the license and proceed for next. These screens are called wizard or window After license page, custom setup wizard will appear where we will do few modifications. First we will change the installation location from OS drive to any other Drive using change option. 1
  7. 7. Say Hello to Java 7 When we will press change button then this window will appear here we have to change location and then press ok button. After changing location again control will come back to previous window, now here we have to perform second operation which is filter Public JRE for this purpose we will click on public JRE and then select don’t install this feature option. Now we are good to proceed for next…. Now installing process will start. This will take few minute.
  8. 8. Say Hello to Java 8 Congratulation!!! Java is successfully installed in our machine. This is last wizard which will appear when Installation will complete successfully.
  9. 9. Say Hello to Java 9 JDK directory structure In java [JDK installed folder] after Installation we will get many file and folder these are jdk1.5.0 ___________|_________________ | | | bin lib jre | | ________|__________ java* tools.jar | | javac* dt.jar bin lib javap* | ________ ___|___ _________ ________ _______ javah* java* | | | | | | javadoc* rt.jar ext security i386 applet fonts charsets.jar | / localedata.jar server client
  10. 10. Say Hello to Java 10 Here important folders are: Bin:  Bin stands for Binary  It keeps binary files  Human can’t understand binary information, its machine understandable codes.  There are different file extension are there, files commonly used extensions are: .exe, .bat, .cmd, .class, .jpeg, .quicktime, .mpg, .mpeg etc  The PATH environment variable should contain an entry for this directory. Lib:  Lib stands for library.  In java lib folder is used for to keep jar file.  Jar stands for Java Archive. JRE:  Jar stands for Java Run time Environment.  Provide platform to run java program.  To run java program we require Run time Environment, JRE is providing that environment to java program.  While installation we didn't install public jre then also we will get JRE folder. This is private JRE which comes along with development tools.  Private and Public JRE: Private JRE is mainly for development purpose. Public JRE is purely for production purpose.  JRE is separate software.  JDK is built on the JRE.
  11. 11. Say Hello to Java 11 JDK = JRE + other concept  contains the source code of JDK.  Whatever code is developed by Sun is present here  We can check internal implementation of Java.This is one of the reasons because java is called as Open Source. Sample Include Demo JDK Other Concept JRE Development Purpose Running purpose Other folder present inside JDK 5.0 Installation Src Java Select folder and open it We can see the source code
  12. 12. Say Hello to Java 12 Steps to Develop Java Program We can divide java program development in 3 cycles. These are 1. Composing 2. Compilation 3. Running
  13. 13. Say Hello to Java 13 Developing Simple Java Program Case 1: Development, compilation, and running from bin folder of JDK 5.0 Step 1: Composing I. Open Notepad (Default Editor) II. Here we have to develop /write java program III. Save the program inside bin folder as it means for the above example file name should be Extension should be .java for every java program Composing step is finished. The outcome of the first step is: .java file Now we will move for next step. class Simple { public static void main(String args[]) { System.out.println("Hello to All"); } }
  14. 14. Say Hello to Java 14 D:/JDK5.0/bin>javac Step 2: Compilation Inside bin folder there are several command or .exe files are available. By making use of these tools we can compile and run our java program. One of common command file is Javac: - javac is a command or tools use to compile java program While compiling java program javac command will perform two tasks 1. Check each and every statement, that it is correct or not. 2. If it is correct then javac command will generate .class file for corresponding to .java file It means after successful compilation we will get .class file. To compile or run java program we required Command Window 1. Open command window (cmd) 2. In command window go where our program is saved Suppose it’s in D:/JDK5.0/bin> Now we will fire command from here D:/JDK5.0/bin>javac When we will press enter, first command window indentify javac command then Second javac command will check program and generate one .class file if program is correct. Command is So the Output of this stage is: .class file Now we will proceed for the next step
  15. 15. Say Hello to Java 15 D:/JDK5.0/bin> java Simple Step 3: Running To run java program we have to use “Java” command/exe Java: It is a command or tool present inside bin folder is JDK, use to run/execute java program. Java command require .class file. Use of Java command to run our program D:/JDK5.0/bin>java Simple Here Location where .class files is available Command Name of class
  16. 16. Say Hello to Java 16 Few Point to remember: While running java file we have to write only name of class (e.g. Simple) compiler automatic take .class as extension. If we will keep .class file extension separately then compiler will treat it as a class name and at last again it will add .class, It will change the name of file, and our program will not execute. So while running java programs only mention the name of File or class Example D:/JDK5.0/bin>java Simple [User Command] Compiler will modify it and treat is as D:/JDK5.0/bin>java Simple.class [compiler command] Now, If we will write D:/JDK5.0/bin>java Simple.class Then compiler will again add .class at end and it will become D:/JDK5.0/bin>java Simple.class.class It is not correct so it will not execute successfully.
  17. 17. Say Hello to Java 17 Example 2: Java Program Step 1: Composing Save as: Location: D:/JDK5.0/bin> Step 2: Compilation Go to saved location (D:/JDK5.0/bin>) and compile the program D:/JDK5.0/bin>javac Here after successful compilation we will get one .class file named Basic.class on D:/JDK5.0/bin> location. Step 3: Running Go to location where .class file is present it means (D:/JDK5.0/bin>) and run the program D:/JDK5.0/bin>java Basic Here after successful execution we will get output as Hello World! class Basic { public static void main(String[] args) { System.out.println("Hello World!"); } }
  18. 18. Say Hello to Java 18 Case 2: Developing Java program outside to bin folder of JDK Up to now we saved the program inside the bin folder, but it’s not proper or advisable way to develop program.  Saving program inside bin folder of JDK and compiling and running from there is very simple any easy. But the problem is  Bin folder of JDK is specific to keeping .exe file it’s should not be use to keep .java and .class file their.  We can save, compile and run the program from outside of bin folder. One better approach is to keep .java and .class file in different location without touching bin folder. Developing Java program outside to the bin folder of JDK We want to compile and run our java program from E:/Lab/Dev but javac.exe and java.exe are present in different location. E:/Lab/Dev> javac Compilation fails Javac.exe Java.exe D:/JDK5.0 bin E:/Lab Dev All .exe/command/tools are present inside the bin folder of JDK5.0 We Kept our program inside E:/Lab/Dev
  19. 19. Say Hello to Java 19 This will not compile successfully because javac present in different folder. Description E:/Lab/Dev> javac When we will hit Enter button then command window will perform two tasks Search/Identify Javac command Execute Javac command We are executing program from E:/Lab/Dev> location so command window will search javac in present location. At the present location javac command is not available so resultant compilation fails. For successful compilation and Running we require updating Path Environment Variable. Some common path environment variables are path classpath java_home j2ee_home ant_home sql_home Updating PATH environment variable E:/Lab/Dev>set PATH=D:/JDK5.0/bin Now if we will compile java program then E:/Lab/Dev> javac Now command window will check from which location I am running. In this situation present working directory(PWD) is E:/Lab/Dev> Then it will check javac command is present in current location/batch/PWD or not. (E:/Lab/Dev> javac
  20. 20. Say Hello to Java 20 If javac command is not available on current location then command window will check is there any PATH environment variable is Updated their? If it is their then command window control will go to Path environment variable location and it will check javac command on that location. If javac command is present their then it will identify it and execute the java program. Command window Job Check command location o Present location o Path environment variable Identify command Run command If we are setting the path environment variable then First it will search particular/given command on Present Working Directory if that command will not present on PWD then only It will go and check PATH environment variable location. Note: If we are setting PATH environment variable through/on command window then sated PATH environment variable will be available for only for that particular command window and when we will close it then sated path will also destroy. It will be not available for other command window. Tip: When we are saving any file then we should give file name in double quotes “ ” with extension. “File_name.extension” It will tell to operating system that doesn’t add any default extension. Generally default extension of any txt file is .txt or .doc.
  21. 21. Say Hello to Java 21 Set the PATH environment variable to bin folder of JDK5.0 We can set PATH environment variable from any location of command window. On Command window go to the location where java program is present. Compile the java program Here two tasks will happen 1. Command Window job It will check from where I am running and It will identify the javac command 2. Javac.exe command job Check the given file name. Check that at PWD its present or not If it is present then it will compile and generate the .class file. Similarly for java.exe command, when we will trigger java command on command window then  Command window job Identify the java command  Java.exe command job Check the class name and then verify it and At last execute it and produce the o/p. While setting path we should write path in capital/upper case letters. Writing path in lower case is proper but not advisable. Writing path in upper case is convention not rules.
  22. 22. Say Hello to Java 22 >Set abc=123 >Echo %abc%; display the present path. O/p 123 >Set new_env=%abc% Here we are taking the value of abc and putting in into new_env. >echo %abc% o/p 123 >echo %new_env% o/p 123 >set new_env=%abc%;0000 echo %new_env% 123;0000 >set new_env=%new_env%;1111 echo %new_env% 123.0000;1111
  23. 23. Say Hello to Java 23 Sr. No Environment variable Reading Env. Variable Result 1 set abc=123 echo %abc% 123 2 set abc=456 do 456 3 set abc=%abc%789 do 456;789 4 Set xyz=%abc%;1011 Echo %xyz% 456;789;1011 5 Do Echo %pqr% %pqr% 6 Set pqr=hello Echo %pqr% Hello 7 Set PATH=”D:/JDK5.0/bin” Echo %path% D:/JDK5.0/bin 8 Set PATH=D:/Mysql/bin do D:/mysql/bin 9 Set PATH=%path%D:/Mysql/bin do D:/JDK5.0/bin;D:/my sql/bin Here in this Table serial no 2 is an example of replacing the previous path environment variable. 3 is an example of adding path environment variable. Here ;( semicolon) is used as a delimiter. Here we can set any number of locations in path environment variable in Path Environment Variable. We can set same path many times. >set Path=abc; >set Path=%Path%;abc; >set Path=%Path%;abc Here in This example we are setting the same path 3 times. It is proper but not advisable; In this condition if any location is not present then compiler will check the same location one after, it will time expensive for compiler. If we will open command window first time and directly we will give command >echo %path% It will print all pre-define path.
  24. 24. Say Hello to Java 24 My computer Environment variable Properties New Path Now we can add new path or replace the existing path. Where is path environment variable. Whatever path we are setting the path inside path command window is local for that particular command window. It will not affect the other or global path environment variable, Its local for that command window. If we want to set path globally then we can go for Keep semicolon (;) between two path locations as delimiter for separation. We can set path Operating system wise through two ways 1. User Level (Specific to one user) 2. System Level (Global to System) My Computer Properties Edit / new / path Environment variable Advanced
  25. 25. Say Hello to Java 25 User level Environment variable: It is available for particular/specific user. If system has 3 user accounts then we can set 3 path environment variables one for every user. These paths are specific to user only these path are not accessible for other user. It provides security. System level Environment variable: This is visible to all user here if we will set path then these path is available for all system users. It is not advisable to use because it is not secure. So we can implement PATH environment variable in three ways: 1. Command windows wise (Local to command window) 2. User level command window (local to one user) 3. System level command window (Global to system) While setting the PATH through command window we should not use space in between if we will give space before or after or in between then it will not work as a PATH environment variable. PATH=123 (Correct no space in between) PATH =123 (Incorrect space is there in between ) PATH= 123 (Incorrect space is there in between ) So we should not use space after and before name and write it as a one word. It will execute successfully but it will not work as a path environment variable because its path with space. Operation system will treat space as a character It will execute successfully but it will not work as a path environment variable because its path with space. Operation system will treat space as a character
  26. 26. Say Hello to Java 26 While setting path through my computer environment variable we should not use space as before Path If we will use space before writing PATH ( PATH) then it will not work as a path environment variable. If we will use path after space then OS will automatically remove it.
  27. 27. Say Hello to Java 27 Case 3: Keeping .java file and .class file in different location: In same directory it is not advisable to keep two different types of file (e.g. .java, .class). In Java we can keep different file in different location. Save as: Location: D:/Dev/src/ Open command window If we will execute/compile normally then compiler will generate .class file at the same location by default. If we want to generate .class file in different location then we can go for command D:/Dev/Src>javac –d ../classes Here after compilation .class file will generate inside classes folder D: Dev Src Classes A.class Class A { public static void main(String args[]) { System.out.println(“Hello”); } }
  28. 28. Say Hello to Java 28 So through –d option we can guide to the compiler to the generate the .class file at desired location. D:/Dev/Src>javac –d ../classes -d option -d option will guide to the compiler that don’t generate .class file at the same location generate it in given location ../classes .. will guide to compiler to go one step up then / classes guide to the compiler that now search classes folder in present location and generate .class file inside this folder
  29. 29. Say Hello to Java 29 e.g. 1: D:/Dev/Src>javac –d .. e.g. 2: D: /Dev/Src>javac -d . e.g. 3: D: /Dev/Src>javac -d ../.. Here .. will guide to the compiler that go one step up from the present location (present location is Src and one step up means parent folder of Src I mean Dev) and generate .class file there One . (dot) represents the present working directory. So here compiler will generate the .class file at the same location It is similar to simple javac command invocation. ../.. ../.. move the control 2 step up and generate .class file at that location
  30. 30. Say Hello to Java 30 e.g. 4: D: /Dev/Src>javac -d sub e.g. 5: D:/Dev/Src>javac -d sub/pack1
  31. 31. Say Hello to Java 31 From present directory to its successive child folder or its parent folder we can reach by using / (slash) and .. or by folder name. So if we want to store or access .class file in different folder in the same directory then we can go for .. and / (slash) This is called relative path We can access only one drive folder using Relative path. Now if we will go for D:/Dev/Src>javac -d ../../../../../../../../.. Here this command will not give error It will generate .class file in D: drive. Using Relative path we can travel only in the present Drive only. Using relative path we can’t generate .class file in different drive. We can only generate .class file in that drive only. If we want to access other Diver folder or want to save/generate .class file from different Driver to different Drive the we can go for Absolute Path. Absolute Path: Through absolute path we can travel anywhere in the system. In absolute path we have to give the complete path of the location. E.g. D:/Dev/Src>javac -d E:/project/code/Classes In this example .class file will generate at E:/project/code/Classes.
  32. 32. Say Hello to Java 32 Table that showing location after compilation according to the above diagram: Sr No Command Invocation from E:/Lab2/Batch/Src Location of .class file 1 Javac Src 2 Javac –d . Src 3 Javac -d Batch 4 Javac -d ../.. Lab2 5 Javac -d ../Classes Classes 6 Javac -d ../../A6 A6 7 Javac -d A2/B2/../B2 B2 8 Javac -d ../../Batch/Src/A2/.. Src 9 Javac -d D:/XYZ/A1/B1 B1 10 Javac -d C:/Lab1/A/B B So whenever we want to use directory or folder of same drive then we can go for relative path or absolute path. If we want to use folder or directory of different Drive then we can use only Absolute path. Here in his situation Relative path is not possible.
  33. 33. Say Hello to Java 33 Tips: If in address bar we are able to see only current folder name and we want to see complete path then we can go for Tools Folder option View Select Display full Path Ok
  34. 34. Say Hello to Java 34 Questions 1. What is the latest Version of JDK? 2. JDK stands for? 3. J2SE stands for? 4. Why exe files are used? 5. What is WIZARD? 6. While installing JDK5.0 will get four items, name them. 7. Which item is mandatory to install? 8. Which item is not mandatory to install? 9. Which item is used for development purpose? 10. Which item is used for customer purpose? 11. Which item is used for production purpose? 12. Which item has to be filtered while installing JDK5.0? 13. Why we will go for EXE files? 14. In which drive we have to keep the development software? 15. Why development languages are not kept inside the C drive? 16. What happens if we install the development language inside the C drive? 17. Currently companies are using which version of JDK? 18. While installing whether we have to give space between JDK and 5.0(Y/N) 19. If we give space between JDK and 5.0 what happens? 20. If we don’t give space between JDK and 5.0 what happens? 21. bin stands for what? 22. What is the use of bin? 23. What kinds of files are kept inside the bin folder? 24. Which are the different types of binary files? 25. Whether binary files are readable by humans? 26. Which type of language is understandable by the computers? 27. Which type of language is understandable by the humans? 28. Name two types of file in bin folder. 29. Lib stands for what? 30. What does lib folder contain? 31. In case of java, what we call for lib folder? 32. What does lib folder contain?
  35. 35. Say Hello to Java 35 33. What does jar stands for? 34. Mention the type of jre. 35. Why private jre is used? 36. Why public jre is used? 37. Why we are filtering the public jre while installing? 38. What stands for jre? 39. jre is a software (Y/N) 40. Whether jre contains bin and lib files? 41. bin and lib files of jre are used for what? 42. For running the java programs which files are used? 43. Where .dll files are located? 44. What is rt.jar? 45. For what rt.jar files are used? 46. What does rt.jar file contain? 47. What is src? 48. What is wrar? 49. For what purpose wrar files are used? 50. In which condition we cannot open the zip file? 51. What we have to do if src is not in a proper format? 52. Whether wrar is used for developing software? 53. How we can unzip the src file? 54. Where .java files are located? 55. .java files are character based files(Y/N) 56. What do you mean by open source? 57. Why java is better than other languages? 58. What do you mean by composing? 59. How to open an editor? 60. How to open a command window? 61. Which command is used for copying? 62. Which command is used for saving the file? 63. In which location we have to save the java file? 64. What is the file name has to be given while saving the java program? 64. By giving which extension we can save the java program? 65. First letter of the class name should be in upper case(Y/N) 66. What happens if we are not saving the file name of java program inside the double quote? 67. Which command is used for compiling the java program?
  36. 36. Say Hello to Java 36 68. Which command is used to run the java program? 69. What is compilation? 70. After compiling what type of file we will get? 71. What do you mean by ‘cd’? 72. Which are the two conditions occurred while compiling the java program? 73. For what .java files are used? 74. For what .class are used? 75. What is the input for javac command? 76. What is the input for java command? 77. For javac command input is .class file(Y/N) 78. For java command input is .java file(Y/N) 79. Why we are not giving any extension to the filename while running the java program? 80. Name common three types of binary files 81. Abbreviate J2ME. 82. Abbreviate J2EE. 83. What is meant by environment variable? 84.Why set command is used? 85. Why set echo is used? 86.Whether the environment variable value is lost while closing the command window? 87. How to set the env-value in a command window permanently? 88. Where we have to go for setting env variable? 89.What is path? 90.How to set path environment variable? 91. If the java code is syntactically correct, then its binary code is stored in which type of file? 92. What is the usage of set command? 93. What is the usage of more than one semi colon in the path environment variable? 94.Updating of path in operating system itself is globally available to all the command window(y/n)? 95. How to synchronize two different directory or drive location? 96.What is classpath? 97. What does command window do? 98.What is need to set the environment variable? 99.Which are the two ways of setting the path? 100.Name the two types of environment variable? 101. What do you mean by system variable?
  37. 37. Say Hello to Java 37 102.What do you mean by user variable? 103.What are the disadvantages of system variable? 104.What are the advantages of user variable? 105.How we can set new user variable? 106.How to update the path environment variable in operating stystem? 107.By using which command we can display the new and existing env- var? 108.Before setting the path in operating system, is it possible to run the command in the command window? Always continue the climb. It is possible for you to do whatever you choose, if you first get to know who you are and are willing to work with a power that is greater than ourselves to do it.