Java  Programming     Unit  1   Your  first  Java  Program   Eclipse  IDE   (c)  Yakov  Fain  2014    
During  this  training   course  we’ll  use  the   textbook     “Java  Programming  24-­‐ Hour  Trainer”   by  Yakov  Fain.     (c)  Yakov  Fain  2014    
Why  Learn  Java?   •  Large  code  base  of  already  wriRen  applicaSons   •  9  million  of  professional  Java  developers   •  Lots  of  enterprise  applicaSons  were  developed  and  are   being  developed  in  Java   •  As  per  Tiobe  index  hRp://bit.ly/rItE  Java  remains    one  of   the  most  popular  languages.   •  The  same  program  can  run  on  different  plaYorms   •  Mobile  Android  development  is  done  in  Java   (c)  Yakov  Fain  2014    
JDK  and  JRE   •  Java  Development  Kit  (JDK)  is  required  to  develop   and  run  programs     •  Java  RunSme  Environment  (JRE)  is  required  to  run   programs.       •  Users  must  have  JRE  installed,  developers  –  JDK     •  JDK  includes  JRE   (c)  Yakov  Fain  2014    
Java  SE  and  Java  EE   •  Java  SE:  Java  Standard  EdiSon       •  Java  EE:  Java  Enterprise  EdiSon  (formerly  J2EE)     •  Java  EE  includes  a  set  of  technologies  built  on  top   of  Java  SE:  Servlets,  JSP,  JSF,  EJB,  JMS,  et  al.     •  All  Java  programs  run  inside  the  Java  Virtual   Machine  (JVM)     (c)  Yakov  Fain  2014    
Installing  JDK  on  Windows   Just  press  the  buRons  Next  or  ConSnue  to  complete  the  install.     The  at  the  end,  JavaFX  2  will    be  also  installed,  which  won’t  be  covered  in  this  training.   (c)  Yakov  Fain  2014    
TesSng  the  installaSon   •  Add  the  bin  folder  to  the  PATH  system   variable  of  your  computer  (for  details  see   hRp://java.com/en/download/help/path.xml).       •  Make  sure  that  the  newly  installed  version  is   being  used.  Open  Command  (or  Terminal  on   MAC)  window  and  type  the  following:     java  -­‐version     (c)  Yakov  Fain  2014    
Walkthrough  1   1.  Download  Java  SE  7  (Currently  JDK  7  u51  –  update  51  from   hRp://www.oracle.com/technetwork/java/javase/downloads                      Download  JDK,  not  JRE.  In  Windows,  download  the  X86  version  of  Java.     2.  Install  Java  SE  (for  details,  see  Lesson  1  of  the  textbook).      To  install  Java  SE  7  on  MAC  computers  download  the  JDK  dmg  file  from   hRp://jdk7.java.net/download.html  ,  run  it  and  follow  the  instrucSons.       3.  If  amer  typing    java  –version  in  the  Command  window  (Terminal  window  on   MAC  OS)-­‐    the  reported  version  should  be  1.7….  If  not,  you  may  need  to  modify   the  system  variable  PATH  to  include  the  bin  directory  of  JDK.       JDK  8  will  be  released  in  March  of  2014.     (c)  Yakov  Fain  2014    
Three  steps  to  run  the  Java  program     •  Write  the  program  and  save  it  in  a  file  with  the   name  that  ends  with  .java,  for  example   HelloWorld.java     •  Compile  the  program  using  javac  compiler,  e.g.   javac  HelloWorld.java     This  will  create  a  file  HelloWorld.class     •  Run  your  program:     java  HelloWorld   (c)  Yakov  Fain  2014    
Compiling  and  running  HelloWorld   (c)  Yakov  Fain  2014    
HelloWorld.java   public class HelloWorld {!  ! public static void main(String[] args){ System.out.println(“Hello World”);! }! }!   In  Java,  you  start  with  creaSng  a  class.  Our  class  is  called  HelloWorld.     A  class  can  have  methods.  In  this  case  it’s  called  main()     You  can  run  the  class  as  a  program  if  it  has  a  method  with  the  following  signature       public static void main(String[] args)!     (c)  Yakov  Fain  2014     !
Walkthrough  2   •  Create  a  directory  called  Prac-calJava  and  a   subdirectory  Lesson1.     •  Open  a  plain  text  editor,  e.g.  Notepad,    enter  the  text   of  the  HelloWorld  program  from  previous  slidw  and   save  it  as  HelloWorld.java  in  the  directory  Lesson1.     •  Open  the  command  window,  change  the  directory  to   Lesson1  using  the    cd  command.     •  Compile  (javac)  and  run  (java)  HelloWorld  program  –  it   has  to  print  HelloWorld  on  the  screen.       (c)  Yakov  Fain  2014    
Eclipse  IDE   •  Integrated  Development  Environment  (IDE)   makes  your  work  more  producSve     •  Includes  text  editor,  compiler,  debugger,  context-­‐ sensiSve  help,  works  with  different  Java  SDKs     •  Eclipse  is  the  most  widely  used  IDE     •  AlternaSves:  IntelliJ  IDEA  (JetBrains),  NetBeans   (Oracle)   (c)  Yakov  Fain  2014    
(c)  Yakov  Fain  2014    
Eclipse  IDE   •  Eclipse  for  Java  EE  Developers  comes  with  a   number  of  plugins  that  will  be  used  in  second   half  of  this  course.     •  The  latest  version  of  Eclipse  IDE  is  called  Kepler.     •  Eclipse  is  also  a  plaYorm  for  plugin  development     –  addiSonal  features  simplifying  programming.   (c)  Yakov  Fain  2014    
Walkthrough  3   •  Download  and  install  the  latest    Eclipse  for  Java  EE    (32  Bit  version)   from  hRp://www.eclipse.org/downloads.       To  install  Eclipse  simply  unzip  the  content  of  the  archive  file  you   downloaded.    To  start  Eclipse,  double-­‐click  on  Eclipse.exe  (or   Eclipse.app  on  MAC).       •  Create  a  new  Java  project  named  Hello  by  using  the  menu  File  |   New  (see  Lesson  2  in  the  textbook  for  details).     •  Create  a  new  Java  class  named  HelloWorld  and  enter  the  code  from   LisSng  1-­‐1  of  the  textbook.     •  Press  Control-­‐S  to  save  and  compile  the  code   .   •  Run  the  program  –  right-­‐click  on  HelloWorld  and  select  the  Run  As   menu  item.     (c)  Yakov  Fain  2014    
Homework   1.  Write  a  program  that  will  print  your  name  and  address  in  the  Console  view  of   Eclipse,  for  example:   Alex  Johnson   23  Main  Street   import  java.uSl.Scanner;   New  York,  NY  10001     public  class  FriendsAndFamily  {   USA    public  staSc  void  main(String[]  args)  {           2.  Study  all  materials  from      Scanner  input=  new  Scanner(System.in);         Lesson  1  and  2  from  the  textbook    do  {        System.out.println("n  Enter  list  price:  "  );      double  listPrice  =  input.nextDouble();     3.  Create  a  new  project  in  Eclipse                  called  Sale      System.out.println("  Enter  discount  %:  "  );        int  discount  =  input.nextInt();     4.  Enter,  compile,  and  run  the              System.out.prinY("  You'll  pay  only    $%2.2f",  listPrice  –     program  FriendsAndFamily.  Try  to     guess,  how  this  program  works.                                                                                                                                                                listPrice*discount/100);                    }  while  (true);      }   5.  Study  Eclipse  tutorial  at     }   hRp://bit.ly/EJSCx     (c)  Yakov  Fain  2014    
AddiSonal  Read   •  How  to  study  online  (the  original,  in  Russian):   hRp://www.stratoplan.ru/lib/break-­‐the-­‐ice/       The  English  translaSon  done  by  Google   Translate:  hRp://bit.ly/10LXgrH         •  Eclipse  IDE  Workbench  User  Guide:   hRp://help.eclipse.org/kepler/index.jsp     (c)  Yakov  Fain  2014    

Java Intro: Unit1. Hello World

  • 1.
    Java  Programming     Unit  1   Your  first  Java  Program   Eclipse  IDE   (c)  Yakov  Fain  2014    
  • 2.
    During  this  training   course  we’ll  use  the   textbook     “Java  Programming  24-­‐ Hour  Trainer”   by  Yakov  Fain.     (c)  Yakov  Fain  2014    
  • 3.
    Why  Learn  Java?   •  Large  code  base  of  already  wriRen  applicaSons   •  9  million  of  professional  Java  developers   •  Lots  of  enterprise  applicaSons  were  developed  and  are   being  developed  in  Java   •  As  per  Tiobe  index  hRp://bit.ly/rItE  Java  remains    one  of   the  most  popular  languages.   •  The  same  program  can  run  on  different  plaYorms   •  Mobile  Android  development  is  done  in  Java   (c)  Yakov  Fain  2014    
  • 4.
    JDK  and  JRE   •  Java  Development  Kit  (JDK)  is  required  to  develop   and  run  programs     •  Java  RunSme  Environment  (JRE)  is  required  to  run   programs.       •  Users  must  have  JRE  installed,  developers  –  JDK     •  JDK  includes  JRE   (c)  Yakov  Fain  2014    
  • 5.
    Java  SE  and  Java  EE   •  Java  SE:  Java  Standard  EdiSon       •  Java  EE:  Java  Enterprise  EdiSon  (formerly  J2EE)     •  Java  EE  includes  a  set  of  technologies  built  on  top   of  Java  SE:  Servlets,  JSP,  JSF,  EJB,  JMS,  et  al.     •  All  Java  programs  run  inside  the  Java  Virtual   Machine  (JVM)     (c)  Yakov  Fain  2014    
  • 6.
    Installing  JDK  on  Windows   Just  press  the  buRons  Next  or  ConSnue  to  complete  the  install.     The  at  the  end,  JavaFX  2  will    be  also  installed,  which  won’t  be  covered  in  this  training.   (c)  Yakov  Fain  2014    
  • 7.
    TesSng  the  installaSon   •  Add  the  bin  folder  to  the  PATH  system   variable  of  your  computer  (for  details  see   hRp://java.com/en/download/help/path.xml).       •  Make  sure  that  the  newly  installed  version  is   being  used.  Open  Command  (or  Terminal  on   MAC)  window  and  type  the  following:     java  -­‐version     (c)  Yakov  Fain  2014    
  • 8.
    Walkthrough  1   1. Download  Java  SE  7  (Currently  JDK  7  u51  –  update  51  from   hRp://www.oracle.com/technetwork/java/javase/downloads                      Download  JDK,  not  JRE.  In  Windows,  download  the  X86  version  of  Java.     2.  Install  Java  SE  (for  details,  see  Lesson  1  of  the  textbook).      To  install  Java  SE  7  on  MAC  computers  download  the  JDK  dmg  file  from   hRp://jdk7.java.net/download.html  ,  run  it  and  follow  the  instrucSons.       3.  If  amer  typing    java  –version  in  the  Command  window  (Terminal  window  on   MAC  OS)-­‐    the  reported  version  should  be  1.7….  If  not,  you  may  need  to  modify   the  system  variable  PATH  to  include  the  bin  directory  of  JDK.       JDK  8  will  be  released  in  March  of  2014.     (c)  Yakov  Fain  2014    
  • 9.
    Three  steps  to  run  the  Java  program     •  Write  the  program  and  save  it  in  a  file  with  the   name  that  ends  with  .java,  for  example   HelloWorld.java     •  Compile  the  program  using  javac  compiler,  e.g.   javac  HelloWorld.java     This  will  create  a  file  HelloWorld.class     •  Run  your  program:     java  HelloWorld   (c)  Yakov  Fain  2014    
  • 10.
    Compiling  and  running  HelloWorld   (c)  Yakov  Fain  2014    
  • 11.
    HelloWorld.java   public classHelloWorld {!  ! public static void main(String[] args){ System.out.println(“Hello World”);! }! }!   In  Java,  you  start  with  creaSng  a  class.  Our  class  is  called  HelloWorld.     A  class  can  have  methods.  In  this  case  it’s  called  main()     You  can  run  the  class  as  a  program  if  it  has  a  method  with  the  following  signature       public static void main(String[] args)!     (c)  Yakov  Fain  2014     !
  • 12.
    Walkthrough  2   • Create  a  directory  called  Prac-calJava  and  a   subdirectory  Lesson1.     •  Open  a  plain  text  editor,  e.g.  Notepad,    enter  the  text   of  the  HelloWorld  program  from  previous  slidw  and   save  it  as  HelloWorld.java  in  the  directory  Lesson1.     •  Open  the  command  window,  change  the  directory  to   Lesson1  using  the    cd  command.     •  Compile  (javac)  and  run  (java)  HelloWorld  program  –  it   has  to  print  HelloWorld  on  the  screen.       (c)  Yakov  Fain  2014    
  • 13.
    Eclipse  IDE   • Integrated  Development  Environment  (IDE)   makes  your  work  more  producSve     •  Includes  text  editor,  compiler,  debugger,  context-­‐ sensiSve  help,  works  with  different  Java  SDKs     •  Eclipse  is  the  most  widely  used  IDE     •  AlternaSves:  IntelliJ  IDEA  (JetBrains),  NetBeans   (Oracle)   (c)  Yakov  Fain  2014    
  • 14.
    (c)  Yakov  Fain  2014    
  • 15.
    Eclipse  IDE   • Eclipse  for  Java  EE  Developers  comes  with  a   number  of  plugins  that  will  be  used  in  second   half  of  this  course.     •  The  latest  version  of  Eclipse  IDE  is  called  Kepler.     •  Eclipse  is  also  a  plaYorm  for  plugin  development     –  addiSonal  features  simplifying  programming.   (c)  Yakov  Fain  2014    
  • 16.
    Walkthrough  3   • Download  and  install  the  latest    Eclipse  for  Java  EE    (32  Bit  version)   from  hRp://www.eclipse.org/downloads.       To  install  Eclipse  simply  unzip  the  content  of  the  archive  file  you   downloaded.    To  start  Eclipse,  double-­‐click  on  Eclipse.exe  (or   Eclipse.app  on  MAC).       •  Create  a  new  Java  project  named  Hello  by  using  the  menu  File  |   New  (see  Lesson  2  in  the  textbook  for  details).     •  Create  a  new  Java  class  named  HelloWorld  and  enter  the  code  from   LisSng  1-­‐1  of  the  textbook.     •  Press  Control-­‐S  to  save  and  compile  the  code   .   •  Run  the  program  –  right-­‐click  on  HelloWorld  and  select  the  Run  As   menu  item.     (c)  Yakov  Fain  2014    
  • 17.
    Homework   1.  Write  a  program  that  will  print  your  name  and  address  in  the  Console  view  of   Eclipse,  for  example:   Alex  Johnson   23  Main  Street   import  java.uSl.Scanner;   New  York,  NY  10001     public  class  FriendsAndFamily  {   USA    public  staSc  void  main(String[]  args)  {           2.  Study  all  materials  from      Scanner  input=  new  Scanner(System.in);         Lesson  1  and  2  from  the  textbook    do  {        System.out.println("n  Enter  list  price:  "  );      double  listPrice  =  input.nextDouble();     3.  Create  a  new  project  in  Eclipse                  called  Sale      System.out.println("  Enter  discount  %:  "  );        int  discount  =  input.nextInt();     4.  Enter,  compile,  and  run  the              System.out.prinY("  You'll  pay  only    $%2.2f",  listPrice  –     program  FriendsAndFamily.  Try  to     guess,  how  this  program  works.                                                                                                                                                                listPrice*discount/100);                    }  while  (true);      }   5.  Study  Eclipse  tutorial  at     }   hRp://bit.ly/EJSCx     (c)  Yakov  Fain  2014    
  • 18.
    AddiSonal  Read   • How  to  study  online  (the  original,  in  Russian):   hRp://www.stratoplan.ru/lib/break-­‐the-­‐ice/       The  English  translaSon  done  by  Google   Translate:  hRp://bit.ly/10LXgrH         •  Eclipse  IDE  Workbench  User  Guide:   hRp://help.eclipse.org/kepler/index.jsp     (c)  Yakov  Fain  2014