lecture java basics

23
Java Basics by Yuriy Voznyak, Software Engineer eleks.com

Upload: eleksdev

Post on 07-Jan-2017

2.189 views

Category:

Technology


0 download

TRANSCRIPT

Java Basics

by Yuriy Voznyak, Software Engineer

eleks.com

Lecture Goal or Why Java?● Become familiar with one more

great platform;● Introduction to Android

development;● Understand the difference

between Java and another popular languages such as C# or C++

● 30 Billion Devices● ...

Why did Java invent?1. Write Once, Run Everywhere. This old Sun slogan describes very

ambitious goal. Sun wanted to create cross-platform language;2. Java Virtual Machine should run every application in the separate

sandbox with dedicated permission; So, applications and environment should provide better security than with using of unmanaged code

3. Easy distribution over Internet as Java applications or Java Applets;

4. Built-in Multithreading support;5. To replace C/C++ with a better design;

What do I need for Java?● If I want to run Java applications on Desktop, there is a JRE (Java

Runtime Environment);● For servers there is another JRE - Server JRE (Server Java Runtime

Environment). Contains tools for JVM monitoring and some tools required for server applications, but does not include browser integration (the Java plug-in), auto-update, nor an installer;

● I want to develop Java Software: need Oracle JDK (Java Development Kit). Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications;

● I want to learn Java deeper or have some extra needs: there is open-source Java implementation called OpenJDK.

Java EditionsJava Standard Edition (Java SE)

lets you develop and deploy Java applications on desktops and servers, as well as embedded environments. Most common used.

Java Enterprise Edition (Java EE)

Extended overset of java engine, contains specifications for designing and developing of Big Enterprise Network Applications

Java Micro Edition (Java ME)

a subset of Java, designed for devices with limited hardware characteristics.

Java Embedded EditionOracle`s marketing term. Contain some binaries to work with Raspberry Pi or ARM devboards

Java DBJust a implementation of open-source database engine

Java CardTechnology to develop Smart Cards secure applets. Sooo limited and low-level

Marketing sets Specialized

Technologies Structure

Noticeable Java Feature Differences

● Syntax is a bit simpler. Really● Packages instead of namespaces● No default values● Values are passed by value only. But, for reference types, value is

reference. So, you just pass copy of the reverence: is able to modify but unable to assign another reference;

● Methods names begin from capital letter :)● Observer patterns everywhere with anonymous classes● Java runs not only on Windows● Less syntax sugar

Main MethodC#static void Main(string[] args)… or evenstatic void Main()

Javapublic static void main(String[] args)

Standard OutputC#

System.Console.WriteLine("Important Message");

Java

System.out.println("Important Message");

Declaring ConstantsC#const int Constant = 42;

Java[public] static final int CONSTANT = 42;

InheritanceC#class Child : Parent, IDerivable{...}

Javaclass Child extends Parent implements Derivable {...}

Exceptions

1-dimensional Collections

Primitive Typestype size range default value wrapper

byte 1 From +127 to -128 0 Byte

short 2 From +32,767 to -32,768 0 Short

int 4 From +2,147,483,647 to -2,147,483,648 0 Integer

long 8 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808

0L Long

float 4 From 3.402,823,5 E+38 to 1.4 E-45 0.0f Float

double 8 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324

0.0d Double

char 2 All Unicode characters '\u0000' Character

boolean 1 False, true false Boolean

Moar Difference● In Java methods are virtual by default but you can make them final. (In

C# they are sealed by default, but you can make them virtual.)● Generics are completely different between the two; Java generics are just

a compile-time "trick" (but a useful one at that). In C# and .NET generics are maintained at execution time too, and work for value types as well as reference types.

● Java doesn't allow the creation of user-defined value types● Java doesn't support overloading● Java doesn't have anything like LINQ● Partly due to not having delegates, Java doesn't have anything quite like

anonymous methods and lambda expressions (in Java 8 does). Anonymous inner classes usually fill these roles.

● Java doesn't have implicitly typed local variables (var directive)

Please, stop!● Java doesn't have extension methods● The access modifiers are somewhat different - in Java there's (currently)

no direct equivalent of an assembly, so no idea of "internal" visibility; ● Java doesn't have the equivalent of "unsafe" code● Java has no preprocessor directives (#define, #if etc)● Java has no equivalent of C#'s ref and out for passing parameters by

reference● Java has no equivalent of partial types● Java has no unsigned integer types● Java has no language support for a decimal type. However,

java.math.BigDecimal provides similar functionality● Java has no equivalent of nullable value types● Java doesn't have static classes (which don't have any instance

constructors, and can't be used for variables, parameters etc)

Is it all?Of course, no.However…

● in C# there's no equivalent to the "default" visibility in Java which takes account of namespace

● C# interfaces cannot declare fields. In java interfaces can contain static final fields and even implementation (sic!)

● C# doesn't have checked exceptions● C# doesn't have anonymous inner classes● C# doesn't have Java's inner classes at all, in fact - all nested classes

in C# are like Java's static nested classes

How to write Java application?1.

2.

What is JarJAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java.util.jar API in the Java platform. There is no restriction on the name of a JAR file, it can be any legal file name on a particular platform.

In many cases, JAR files are not just simple archives of java classes files and/or resources. They are used as building blocks for applications and extensions. The META-INF directory, if it exists, is used to store package and extension configuration data, including security, versioning, extension and services.

Designing Classes Structureexplicit-representation principle: classes included to reflect natural

categories;no-duplication principle: member functions situated among class

definitions to facilitate sharing;local-view principle: program elements placed to make it easy to see how

the elements interact;look-it-up principle: class definitions including member variables for stable,

frequently requested information;need-to-know principle, with public interfaces designed to restrict member-

variable and member-function access, thus facilitating the improvement and maintenance of nonpublic program elements;

keep-it-simple principle: class and function definitions are broken up when these definitions become too complex to understand;

modularity principle, with program elements divided into logically coherent modules.

Where to Read More1. Documentati

on2. Books3. Internet4. Source Code

Let`s try!

Inspired by Technology.Driven by Value.

Find us at eleks.com

Have a question? Write to [email protected]