SlideShare a Scribd company logo
1 of 21
Zachary Blair
April 10, 2012
   Intro to Java and the JVM
   Basic Types/Arrays
   Classes/Inheritance
   Nested Classes
   Exceptions
   Enums
   Autoboxing/unboxing
   Annotations
   Generics
   Originally developed by Sun for embedded
    devices, version 1.0 released in 1996




   James Gosling (Father of Java, Officer of the
    Order of Canada),  photo by Peter Campbell
   Intended as an alternative to C++ that is:
    ◦   Simpler
    ◦   Higher-level
    ◦   Multithreaded
    ◦   Dynamic
    ◦   Object-oriented
    ◦   Trivially portable (“Write Once, Run Everywhere”)

    ◦ No pointer arithmetic
    ◦ Automatic garbage collection
   Emulates a “virtual” CPU             Java Source
                                         Files (.java)


    Executes “bytecode”
                                                javac


    (each opcode is one byte            Java bytecode
    long) stored in ‘.class’             files (.class)

    files

   The same “bytecode” can      x86 Java            ARM Java
    run on any machine with       Virtual             Virtual
                               Machine (JVM)       Machine (JVM)
    a JVM implemented for it
   Not specific to the Java language. Other
    languages compile for the JVM:

    ◦   Groovy
    ◦   Clojure
    ◦   Jython
    ◦   JRuby
    ◦   Dozens of others…
   Similar to C:
    byte              ->   8 bits
    short             ->   16 bits
    int               ->   32 bits
    long              ->   64 bits
    float             ->   32 bits
    double            ->   64 bits
    char              ->   16 bits
    boolean
   Numeric types are always signed
   ‘char’ is a 16 bits instead of 8!
   No conversion between int and boolean as in C++.
    if(0) or while(1) are compile-time errors in Java.
Eight bytes walk into a bar.  The bartender
       asks, “Can I get you anything?”


“Yeah,” reply the bytes.  “Make us a double.”


            ** I didn’t come up with this joke, but it’s comedy Gold.
   Similar to C in syntax
    int[] numbers = new int[10];
    numbers[0] = 1000;
   Arrays are Objects with members and
    methods (member functions)!
    for (int i = 0; i < numbers.length; i++) {
         System.out.println(numbers[i]);
    }

   Trying to index past the end of an array
    results in ArrayIndexOutOfBoundsException
public class Vector2D
{
     public int x;
     public int y;
     public float magnitude()
     {
          return Math.sqrt(x * x + y * y);
     }
}
   Method implementations must be defined in the
    class body. No ‘.h’ files, just ‘.java’!
   Classes themselves can have access modifiers!
   Each ‘public’ class must reside in a ‘.java’ file of
    the same name (e.g. Vector2D.java).
public class Velocity extends Point2D
    {
         public boolean isTooFast()
         {
              return (magnitude() > 60);
         }
    }

   Uses ‘extends’ to specify a base class
   Only single inheritance is supported
   All inheritance equivalent to ‘public’ in C++
   All classes implicitly inherit from Object
public class OuterClass
    {
         public int var;
         class InnerClass
         {
              public void foo()
              {
                   System.out.println(var);
              }
         }
    }

   Non-static inner classes have access to the
    outer class’s members!
   Instantiated using ‘this.new’ instead of ‘new’.
public void foo()
         {
              class Point
              {
                   public int x;
                   public int y;
              }

              Point p = new Point();
         }


   You can even declare a class inside a method!
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.out.println(“The button was pressed”);
  }
}


   Defined and instantiated an anonymous
    class that implements an “ActionListener”
    interface, all in the parameter list to a method
    call!

   Used as Java’s alternative to function pointers
    in C for callbacks.
try {
       int a[] = new int[2];
       System.out.println(“A three:" + a[3]);
}
catch (ArrayIndexOutOfBoundsException e) {
     System.out.println("Exception:" + e);
}


    Handle error conditions, similar to C++’s try/catch.
    Some exceptions are ‘checked’, meaning it is a
     compile-time error to not either catch them, or
     explicitly mark your method as possibly throwing
     that exception to its caller.
public enum Color
{
     RED, ORANGE, YELLOW, GREEN, BLUE
}
…
Color c = Color.RED;

   Like C enums, but in Java, enum is a sort of
    class with enum values as static members.
   You can add data and methods to the enum!
Color c = Color.RED;
if (c.isWarmColor())
 System.out.println(c.toString());
int x = 10;
Integer y = new Integer(x);
Integer z = x;

int a = z;

list list = new List();
list.append(x);               // x converted to
Integer
  Automatically convert between primitive types
   (e.g. int, double, char) to their Object-based
   (boxed) types (e.g. Integer, Double, Character).
  Useful because boxed types can be stored in
   collection classes just like any other Object
public class Base
{
     public void foo() { }
}
…
public class Subtype extends Base
{
     @Override public void foo() { bar(); }
}

   @Override marks a method as explicitly
    overriding a base class method, triggering a
    compilation error if it doesn’t!
class<T> Pair
    {
         public T first;
         public T second;
    }

    Pair<int> p = new Pair<int>();
    p.first = 10;
    p.second = 20;

   A bit like templates in C++ (except that
    internally only one implementation is
    created).
   Java has some similar syntax to C++
   Rather than compiling to native code, it
    compiles to bytecode for the JVM to execute
   Java makes it more difficult to make certain
    mistakes (automatic garbage collection and
    no pointer arithmetic).

   Learn more at http://docs.oracle.com/javase/
Intro to Java for C++ Developers

More Related Content

What's hot

JNA - Let's C what it's worth
JNA - Let's C what it's worthJNA - Let's C what it's worth
JNA - Let's C what it's worthIdan Sheinberg
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overviewJulian Król
 
C# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageC# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageJacinto Limjap
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type scriptDmitrii Stoian
 
KafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesKafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesRubén Izquierdo Beviá
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#sudipv
 
CLTL presentation: training an opinion mining system from KAF files using CRF
CLTL presentation: training an opinion mining system from KAF files using CRFCLTL presentation: training an opinion mining system from KAF files using CRF
CLTL presentation: training an opinion mining system from KAF files using CRFRubén Izquierdo Beviá
 
Type hints in python & mypy
Type hints in python & mypyType hints in python & mypy
Type hints in python & mypyAnirudh
 
Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3mametter
 
The D Programming Language - Why I love it!
The D Programming Language - Why I love it!The D Programming Language - Why I love it!
The D Programming Language - Why I love it!ryutenchi
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In PracticeMichiel Borkent
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression TreesExploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Treesrasmuskl
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleMartin Odersky
 

What's hot (19)

JNA - Let's C what it's worth
JNA - Let's C what it's worthJNA - Let's C what it's worth
JNA - Let's C what it's worth
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
 
C# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageC# and the Evolution of a Programming Language
C# and the Evolution of a Programming Language
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 
KafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesKafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF files
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
CLTL presentation: training an opinion mining system from KAF files using CRF
CLTL presentation: training an opinion mining system from KAF files using CRFCLTL presentation: training an opinion mining system from KAF files using CRF
CLTL presentation: training an opinion mining system from KAF files using CRF
 
Type hints in python & mypy
Type hints in python & mypyType hints in python & mypy
Type hints in python & mypy
 
D programming language
D programming languageD programming language
D programming language
 
Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3
 
The D Programming Language - Why I love it!
The D Programming Language - Why I love it!The D Programming Language - Why I love it!
The D Programming Language - Why I love it!
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
TypeScript
TypeScriptTypeScript
TypeScript
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
 
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression TreesExploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simple
 

Similar to Intro to Java for C++ Developers

Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiasanjeeviniindia1186
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsMahika Tutorials
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonEd Austin
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012Tech_MX
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Courseparveen837153
 

Similar to Intro to Java for C++ Developers (20)

Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
core java
core javacore java
core java
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparison
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java
JavaJava
Java
 
Java Notes
Java Notes Java Notes
Java Notes
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 

Recently uploaded

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Intro to Java for C++ Developers

  • 2. Intro to Java and the JVM  Basic Types/Arrays  Classes/Inheritance  Nested Classes  Exceptions  Enums  Autoboxing/unboxing  Annotations  Generics
  • 3. Originally developed by Sun for embedded devices, version 1.0 released in 1996  James Gosling (Father of Java, Officer of the Order of Canada), photo by Peter Campbell
  • 4. Intended as an alternative to C++ that is: ◦ Simpler ◦ Higher-level ◦ Multithreaded ◦ Dynamic ◦ Object-oriented ◦ Trivially portable (“Write Once, Run Everywhere”) ◦ No pointer arithmetic ◦ Automatic garbage collection
  • 5. Emulates a “virtual” CPU Java Source Files (.java) Executes “bytecode” javac  (each opcode is one byte Java bytecode long) stored in ‘.class’ files (.class) files  The same “bytecode” can x86 Java ARM Java run on any machine with Virtual Virtual Machine (JVM) Machine (JVM) a JVM implemented for it
  • 6. Not specific to the Java language. Other languages compile for the JVM: ◦ Groovy ◦ Clojure ◦ Jython ◦ JRuby ◦ Dozens of others…
  • 7. Similar to C: byte -> 8 bits short -> 16 bits int -> 32 bits long -> 64 bits float -> 32 bits double -> 64 bits char -> 16 bits boolean  Numeric types are always signed  ‘char’ is a 16 bits instead of 8!  No conversion between int and boolean as in C++. if(0) or while(1) are compile-time errors in Java.
  • 8. Eight bytes walk into a bar.  The bartender asks, “Can I get you anything?” “Yeah,” reply the bytes.  “Make us a double.” ** I didn’t come up with this joke, but it’s comedy Gold.
  • 9. Similar to C in syntax int[] numbers = new int[10]; numbers[0] = 1000;  Arrays are Objects with members and methods (member functions)! for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }  Trying to index past the end of an array results in ArrayIndexOutOfBoundsException
  • 10. public class Vector2D { public int x; public int y; public float magnitude() { return Math.sqrt(x * x + y * y); } }  Method implementations must be defined in the class body. No ‘.h’ files, just ‘.java’!  Classes themselves can have access modifiers!  Each ‘public’ class must reside in a ‘.java’ file of the same name (e.g. Vector2D.java).
  • 11. public class Velocity extends Point2D { public boolean isTooFast() { return (magnitude() > 60); } }  Uses ‘extends’ to specify a base class  Only single inheritance is supported  All inheritance equivalent to ‘public’ in C++  All classes implicitly inherit from Object
  • 12. public class OuterClass { public int var; class InnerClass { public void foo() { System.out.println(var); } } }  Non-static inner classes have access to the outer class’s members!  Instantiated using ‘this.new’ instead of ‘new’.
  • 13. public void foo() { class Point { public int x; public int y; } Point p = new Point(); }  You can even declare a class inside a method!
  • 14. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“The button was pressed”); } }  Defined and instantiated an anonymous class that implements an “ActionListener” interface, all in the parameter list to a method call!  Used as Java’s alternative to function pointers in C for callbacks.
  • 15. try { int a[] = new int[2]; System.out.println(“A three:" + a[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Exception:" + e); }  Handle error conditions, similar to C++’s try/catch.  Some exceptions are ‘checked’, meaning it is a compile-time error to not either catch them, or explicitly mark your method as possibly throwing that exception to its caller.
  • 16. public enum Color { RED, ORANGE, YELLOW, GREEN, BLUE } … Color c = Color.RED;  Like C enums, but in Java, enum is a sort of class with enum values as static members.  You can add data and methods to the enum! Color c = Color.RED; if (c.isWarmColor()) System.out.println(c.toString());
  • 17. int x = 10; Integer y = new Integer(x); Integer z = x; int a = z; list list = new List(); list.append(x); // x converted to Integer  Automatically convert between primitive types (e.g. int, double, char) to their Object-based (boxed) types (e.g. Integer, Double, Character).  Useful because boxed types can be stored in collection classes just like any other Object
  • 18. public class Base { public void foo() { } } … public class Subtype extends Base { @Override public void foo() { bar(); } }  @Override marks a method as explicitly overriding a base class method, triggering a compilation error if it doesn’t!
  • 19. class<T> Pair { public T first; public T second; } Pair<int> p = new Pair<int>(); p.first = 10; p.second = 20;  A bit like templates in C++ (except that internally only one implementation is created).
  • 20. Java has some similar syntax to C++  Rather than compiling to native code, it compiles to bytecode for the JVM to execute  Java makes it more difficult to make certain mistakes (automatic garbage collection and no pointer arithmetic).  Learn more at http://docs.oracle.com/javase/