Introduction to Java

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

16 Terms

1
New cards

Virtual Machine

a software emulation of a physical computer that runs programs in an isolated environment

VMs handle memory allocation and garbage collection, reducing the risk of memory-related errors 

execution within a VM provides a layer of isolation

2
New cards

Virtual Machine Based Languages

Java (JVM), C# (CLR), Python (Cpython, Jython, IronPython), Kotlin, JS, Ruby…

instead of directly running on hardware, VM-based languages run on a software-based virtual environment

code written in VM-based languages can run on any system with the appropriate VM installed 

3
New cards

Java

A modern, platform independent, object-oriented programming language

  • Developed by SUN microsystems in 1995 (James Gosling). Java is now owned by Oracle

  • Overarching design goal: simplicity and reuse

  • Originally designed for embedded devices… the first envisioned application was the Java toaster

  • Next it was the web language of choice (applets) 

  • Since evolved into a programming language of choice for high reliability and good performance. Now commonplace in: 

    • Enterprise Systems (IBM/Oracle)

    • Mobile Devices (Android) 

    • Embedded Devices (SmartTVs, IoT) 

    • Financial, Medical, and Automotive domains

4
New cards

Open

Java is a modern, platform independent, object-oriented programming language

  • Open standards allow interoperation and promote innovation

5
New cards

Abstract

Java is a modern, platform independent, object-oriented programming language

  • Java uses a virtual machine to abstract over device operating systems

6
New cards

Java Virtual Machine (JVM)

Abstracts over a device hardware

  • Processor

  • Memory

  • Input / Output 

  • Graphical Interfaces…

Contains a virtual computer processor

7
New cards

Virtual Computer Processor

  • Executes its own machine language known as bytecode

  • Very simple instructions, such as add, multiply, compare (c.f. machine language) 

  • Java programs are compiled into bytecode by the developer

  • The JVM interprets these into whatever the hardware understands at run time

  • Java bytecode is an example of an intermediate language (neither something you write in, nor something that is directly executed by a computer)

8
New cards

Java Machine Language - Bytecode

<p></p>
9
New cards

C++ vs Java

  • I ran a Bubble Sort algorithm on an array of 13 integers…

  • Results shown in nanoseconds (1000 nsec = 0.001 msec)

    • Java: 4279 nsec

    • C++: 666 nsec (6 times faster)

10
New cards

Language

based on the syntax of C and C++

public class HelloWorld
{
	public static void main(String[] arg)
	{
		System.out.println("Hello 
		World");
	}
}

11
New cards

Classes

  • Every Java program is made up of one or more

    • Java’s unit of modularity… they define objects 

  • The class keyword defines a unique name for the class you’re writing. Curly braces define the code that is part of that class

  • One class per file, with filename matching as the class name

public class HelloWorld
{

}

12
New cards

Methods

  • The main method defines the start point for your program

    • This method always returns void in Java (they have no return value)

    • Parameter is an array of strings (command line arguments, like C)

  • Methods are like functions in C

    • They are blocks of code that have names, parameters and return types 

public static void main(String[] args)
{
	//say what you want it to do
}

13
New cards

Comments

A single line comment begins with //

  • All text on the line after the // characters are ignored

  • Java also supports the C style /* */ comment blocks

//la la la

14
New cards

Statements

  • Method invocation (like calling a function in C) statements end with a semicolon, as they do in C and C++

  • For example: 

    • System.out is the name of an object (the console output stream) 

    • println is the name of a method – note there’s also a print method

System.out.println("Hello World");

15
New cards

Code Blocks

  • The end of the method and end of the class (respectively)

  • As with C, it's very important to maintain good code indentation

  • In Java, methods are always defined inside classes

{
	{
	
	}
}

16
New cards

Compiling a Java Program

  • Create a text file with your favourite editor (e.g. VS Code)

    • Filename must match the name of the class it contains

    • Filename must end in .java

  • Open a command line prompt 

    • shell in Unix 

    • cmd in Windows 

  • Change directory to the location of your file, then compile your program into bytecode:

    • javac HelloWorld.java 

  • Now start a Java virtual machine that interprets your program: 

    • java HelloWorld