[Go to site: main page, start]

0% found this document useful (0 votes)
22 views5 pages

Java Programming Essentials Guide

The document provides an overview of Java programming concepts including: 1. Downloading the Java Development Kit (JDK) which includes a compiler and runtime environment to build Java applications. 2. Using a code editor like NetBeans, IntelliJ, or Eclipse to write Java code. 3. The anatomy of Java programs including functions, classes, methods, and the main method which acts as the program entry point. 4. Writing a simple "Hello World" Java program as an example. 5. How Java code is compiled into bytecode and executed.

Uploaded by

andre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Java Programming Essentials Guide

The document provides an overview of Java programming concepts including: 1. Downloading the Java Development Kit (JDK) which includes a compiler and runtime environment to build Java applications. 2. Using a code editor like NetBeans, IntelliJ, or Eclipse to write Java code. 3. The anatomy of Java programs including functions, classes, methods, and the main method which acts as the program entry point. 4. Writing a simple "Hello World" Java program as an example. 5. How Java code is compiled into bytecode and executed.

Uploaded by

andre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming

Download Jdk: Java Development Kit

It is a software for building Java Applications, it has a compiler, it has a bunch of


code that we can run, it has a Java runtime environment.

After that, we will need a Code Editor, like NetBeans, IntelliJ, Eclipse.

The Anatomy of a Java Programs:

Functions: A Function is a block of code that performs a task. As a metaphor, think


about your tv control, each button performs a task, Functions in programming
language are exactly the same.

We start specifying the Returning type of this Function Returntype. Some Functions
return a value, like a number, day time and so on. Other functions don’t return
anything, so the Returntype of this Function is Void. Void is a reserved key word in
Java.

Then we have the name of our Function. We have to name our function with a
specified name like sendEmail, it’s clear that we are going to send an Email with this
Function. After that we have a pair of parentheses and inside these parentheses in
add the paremeters. We use these parentheses to give value to a Function. And
then we have the pair of curly braces and inside of these braces we write the actual
Java Code. We put the left brace on the same line where we define our function.

Main is the entry code to our programs.

A Class is a container for one or more related functions.


A Method is a function that is part of a Class.
Access Modifier is a special keyword that determines if other classes and methods
in this program can access these classes and methods.

public class Main {


public void main () {

}
}

At a minimum we have a Main class, and inside this Main class we have the Main
method.

Your first Java Program:

In Java textual data we always use double quotes: “”


String: is a sequence of characters .

public class Main {


public void main () {
[Link] (“Hello World”);

}
}

Result: Hello World

How Java Code gets executed:


There are two steps involved, which are Compilation and Execution.

In the Compilation step, Netbeans uses the Java Compiler to compile our code into a
different format called Java bytecode.
We have four editions of Java for building different kinds of application.

We have Java Standard Edition, this is the core of the platform. It contains all of the
libraries that every Java developer must learn.
We have Java Enterprise Edition, which is used for building very large-scale and
distributed systems. It’s built on top of Java Standard Edition and provides additional
libraries for building fault tolerant, distributed multi-tiered software.
We have Java micro Edition, which is a subset of java Standard Edition designed
for mobile advices, it has libraries specific to mobile devices.
We have Java Card, which is used in smart cards

Variables: We use Variables to temporary store data in computer’s memory, to


store these variables we use integer (int).
Integers are whole numbers, numbers that don’t have a decimal point.
Then we give the variables a name, will be its identifier. Because we use to identify
our variable. The equal sign is called the assignment operator.

public static void main(String[] args) {


int myAge = 30
int herAge = myAge

[Link](herAge);
}

Result = 30

Compilers: It’s a computer program, will take your source code and then compile to
binary file.
Interpreters: Is a language processer which converts high-level programming to
low-level language.

Primitive Types:

Primitive types: for storing simple values


Non-Primitive types (References): for storing complex objects. Like data objects or
mail messages.
Primitive types:
we have byte that takes one memory and in one bite we can store values from -128
to 127. The more bytes we have, the larger numbers we can store.
Short: which takes 2 bytes and with this we can store values up to 32,000.
Integer: takes 4 bytes of memory and we can store values up to 2 billion.
Long: which takes 8 bytes of memory and it can store values up to 2 billion, if you
want to store values higher than 2B we have to add L at the end.
Float: used for decimal numbers (such as 3,2; 10,50… and so on). It takes 4 bytes
Add F at the end if it has an error message.
Double: used for decimal numbers (such as 3,2; 10,50… and so on). It takes 8 bytes
of memory
Char: for storing a single character like ABC, it takes 2 bytes of memory. To use this
character, we have to use just one quotes. Like this ‘A’. A string represents a series
of characters.
Boolean: for storing Boolean values which can be true or false.

Always use descriptive names for your variables, so you won’t be lost on your own
codes.

Reference Types:
Ex:

package helloworld;

import [Link];
/**
*
* @author andregoncalves
*/
public class HelloWorld {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
byte age = 30;
Date now = new Date();
[Link](now);

Result: actual date.

Strings: The string is a string literal, that means a string value.


Strings are reference types in Java.
Strings in Java are immutable. We cannot mutate them, we cannot change them.

[Link] + … : check if a string ends with something mentioned before.


[Link] + … : check if a string starts with something mentioned before.
[Link]([Link]("H");
Result: true

[Link]: to check the length of the message (how many characters has on
it). This is useful in situations where you want to check the length of the input by the
user.
[Link]: It gives you the index of what was written. Like for the Hello
World, if you put H, will appear 0 in the output because H is in number 0 position.
The index of the first position is 0.
[Link]: You can replace one or more characters or something else.

String message = "Hello World" + "!!";


[Link]([Link]("!", "*");
Result: Hello World** instead of Hello World!!

[Link]: it will give all the characters a lower case.


[Link]: it will give all the characters an UPPER case.

[Link]: it will get rid of all the extra spaces that was given in our code.

Escape Sequences: There will be times when we are going to use special
characters like “” double quotes or () strings in our code, like a tab, or a new line
or a / backslash.

We have String message = "Hello Mosh"; let’s say that we want surround Mosh with
double quotes, but there’s a problem if we add a quote, the Java compiler will think
that is the termination of our string, so it won’t understand what we have after. To fix
this problem, we have to prefix this double code with a backslash.

“Hello \”Mosh\””

We use Enum for things that don’t change, instead of using an array we user
enum, which is cleaner.

Common questions

Powered by AI

The compilation and execution of a Java program involve two main steps. Initially, the source code written in Java is converted into bytecode by the Java Compiler during the compilation phase. This bytecode is a platform-independent intermediate representation that can be executed on any platform with a Java Virtual Machine (JVM). In the execution phase, the JVM interprets or compiles this bytecode into machine code specific to the host platform, allowing the program to run. Java bytecode's significance lies in its ability to allow Java programs to be portable across different operating systems .

In Java, strings are immutable, meaning they cannot be modified once created. This immutability ensures thread-safety and consistent hash code values, which is critical for using strings as keys in hash-based collections. Although it means every string operation, like concatenation or modification, effectively creates a new string object, this can lead to increased memory usage and processing time. Understanding immutability helps developers use strings efficiently, often by employing the StringBuilder class for multiple modifications. Immutability also simplifies sharing and caching strings across different areas of a program .

The Java Development Kit (JDK) is essential for building Java applications as it includes the Java Compiler, a runtime environment, and various libraries that enable compilation and execution of Java programs. A code editor such as NetBeans, IntelliJ, or Eclipse complements the JDK by providing an integrated development environment (IDE) where developers can write, organize, and manage the code more efficiently. The JDK handles the technical aspects of code execution while the code editor offers a user-friendly interface and tools for software development .

Escape sequences in Java are used to insert special characters into string literals without breaking the syntax. They precede characters such as quotes, newlines, and backslashes with a backslash ('\') to indicate that the character should be treated differently. For instance, to include double quotes within a string, you use the escape sequence \". Example: String text = "This is a \"quoted\" word."; This allows the Java compiler to understand the quotes should appear in the output string, not as delimiters of the literal .

Using descriptive names for variables in Java is important because it improves code readability, maintainability, and understandability. Descriptive names clearly convey the variable's purpose and the kind of data it holds, making it easier for developers to follow the logic and intent of the code. Not following this practice can lead to confusion, increased likelihood of errors, and difficulty in collaborating on or extending code, as other developers may struggle to understand the code's functionality and intent. Well-named variables help maintain clarity, especially in large and complex programs .

Java supports multiple integer types, each with varying memory requirements and range. A 'byte' uses 1 byte, storing values from -128 to 127. 'Short' occupies 2 bytes and stores values up to 32,000. An 'int' consumes 4 bytes, allowing storage up to 2 billion, and a 'long' uses 8 bytes, also allowing for values beyond 2 billion if an 'L' is added. The choice of integer type impacts memory usage and efficiency; smaller types conserve memory but have restricted value ranges, while larger types offer broader ranges but consume more memory .

Java Standard Edition (SE) is the core platform that provides essential libraries for general-purpose programming. It is suited for desktop and simple server applications. Java Enterprise Edition (EE) builds upon SE and offers additional libraries for developing large-scale, distributed, fault-tolerant, and multi-tiered applications such as enterprise-level systems. Java Micro Edition (ME) is tailored for mobile and embedded device applications, featuring libraries optimized for small-scale environments. Java Card targets smart cards and similar devices requiring secure and efficient execution of Java applications in constrained environments .

Primitive types in Java, such as int, byte, short, and char, store basic data values like numbers and characters directly in memory, providing efficient storage and quick access. Conversely, reference types, including classes, arrays, and interface types, store references (or addresses) to objects in memory. Understanding these distinctions is crucial, as primitive types deal directly with data, whereas reference types manipulate objects and their behaviors. This knowledge impacts memory management, performance optimization, and understanding of object-oriented programming principles .

In Java, a function is a block of code that performs a specific task. When a function is defined within a class, it is called a method. Methods are functions associated with classes that define their behavior. Access modifiers, such as public, private, and protected, control the visibility and accessibility of these methods. For example, a 'public' modifier makes a method accessible from outside its class, whereas a 'private' modifier restricts access to within the class itself .

Java methods like 'message.replace' and 'message.toUpperCase' are powerful for dynamically altering strings. 'message.replace' allows for replacing specific characters or substrings within a string, useful for tasks such as sanitizing input data or formatting text for specific outputs. For instance, it can replace commas with semicolons in CSV data. 'message.toUpperCase' converts all characters to uppercase, useful in scenarios like normalizing user input for case-insensitive comparisons or preparing text for environments where uppercase formatting is necessary. These methods enhance the flexibility and usability of string operations in Java applications .

You might also like