100% found this document useful (2 votes)
55 views

Java Quick Overview

This document provides a quick overview of key Java concepts including: 1. The basic structure of a Java program with a main method 2. Common data types like int, float, and String 3. How to use arrays, including multi-dimensional arrays, and common String methods 4. Key concepts like functions, exceptions, classes, objects, inheritance and interfaces.

Uploaded by

naxoland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
100% found this document useful (2 votes)
55 views

Java Quick Overview

This document provides a quick overview of key Java concepts including: 1. The basic structure of a Java program with a main method 2. Common data types like int, float, and String 3. How to use arrays, including multi-dimensional arrays, and common String methods 4. Key concepts like functions, exceptions, classes, objects, inheritance and interfaces.

Uploaded by

naxoland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 2

Java quick overview 9.

Arrays in Java

1. Basic structure of a Java program int[] data = new int[10]; // Array to store 10 integer values
...
public class MyClass for (int i = 0; i < data.length; i++)
{ ...
public static void main(String[] args)
{ int[] someData = {10, 11, 12}; // Array with pre-defined data
// Main code of our program
} int[][] data2 = new int[5][]; // Array with 5 rows
} data2[0] = new int[3]; // Row 1 has 3 columns
data2[1] = new int[10]; // Row 2 has 10 columns
...
2. Basic data types: for (int i = 0; i < data2.length; i++)
for (int j = 0; j < data2[i].length; j++)
byte, short, int, long // float, double // char // boolean // String ...

3. Convert to number 10. Strings in Java

int number = Integer.parseInt("42"); • charAt method to get a specific character: text.charAt(i)


float floatNumber = Float.parseFloat("3.1416");
double doubleNumber = Double.parseDouble("3.141592654");
• compareTo to compare two strings alphabetically. Returns negative, 0 or positive number if
first text is lower, equal or greater than the second text, respectively:
4. Operators if (text1.compareTo(text2) > 0)...
• equals / equalsIgnoreCase t o d e t e r m i n e i f t w o s t r i n g s a r e t h e s a m e :
if (text1.equals(text2))...
• Arithmetic: +, -, *, /, %, ++, –
• indexOf / lastIndexOf to look for a subtext in a text (returns the position where it is found, or
• Assignments: =, +=, -=, *=, /=, %=
-1 if it was not found): int pos = text1.indexOf("hello");
• Comparisons: >, >=, <, <=, ==, != (do not use with Strings, use equals instead)
• contains to check if a text contains a subtext (text.contains(subtext)), and startsWith /
• Logical: &&, ||, !
endsWith to check if a text starts or ends with a given subtext (text.endsWith(subtext))
• toUpperCase / toLowerCase to convert the whole string to upper / lower case:
5. Basic input String result = text.toUpperCase();
• substring so cut from the specified start index (inclusive) to the specified end index
import java.util.Scanner;
... (exclusive). If no end index is speficied, it cuts until the end of the string.
Scanner sc = new Scanner(System.in); String subtext = text.substring(3, 5);
int number = sc.nextInt(); • replace to replace all the occurrences of an old substring with a new substring in a given
String text = sc.nextLine(); text: String result = text.replace("old", "new");
sc.close();
• split to split a string in an array using a delimiter: String[] parts = text.split(",");
6. Basic output
11. Functions in Java
System.out.println("The result is " + result);
System.out.print("The result is " + result); public static String myFunction()
System.out.printf("The result is %.2f", result); {
return "Hello";
}
7. Constants
public static void myOtherFunction (int a, String b)
class MyClass {
{ ...
public static final int MAX_USERS = 10; }

8. foreach in Java 12. Exception handling


for (int number: numbers)
System.out.println("" + number);
Catching exceptions:

try
{
// Code that may fail
} catch (NumberFormatException e1) { 15. Inheritance
// Error message for number format
} catch (DivideByZeroException e2) { public class Dog extends Animal
// Error message for dividing by zero
...
} catch (Exception eN) { Methods that can be overriden from Object:
// Error message for any other error
} @Override @Override
public boolean equals(Object o) public String toString()
Throwing exceptions:
We can use "super" to call methods and constructors from parent class
public static void a() throws InterruptedException
{ public Dog() {
throw new InterruptedException ("Exception in a"); super()
} ...

try
{
16. Abstract classes
a();
} catch (...) public abstract class Animal {
...
public abstract void talk();
}
13. Defining classes and objects

class Person
17. Interfaces
{
String name; public interface Shape { public class Circle implements Shape
int age; float calculateArea();
void draw();
public Person(String name, int age) }
{
this.name = name; 18. Anonymous classes
this.age = age;
} Person[] people = new Person[10];
...
public String getName() Arrays.sort(people, new Comparator<Person>()
{ {
return name; @Override
} public int compare(Person p1, Person p2)
{
public void setName(String name) return Integer.compare(p1.getAge(), p2.getAge())
{ }
this.name = name; });
}

… 19. Collections
}
List<String> myList = new ArrayList<>();
Person p = new Person("Nacho", 40); myList.add("Hello");
System.out.println(p.getName()); for (int i = 0; i < myList.size(); i++)
System.out.println(myList.get(i));
14. Visibility
Map<String, Person> data = new HashMap<>();
data.put("1111111A", new Person("Nacho", 40));
public: everyone can see the element for (String key: data.keySet())
protected: only subclasses and classes from the same package System.out.println(data.get(key));
private: only the own class
Set<String> mySet = new HashSet<>();
package (default value): only classes from the same package mySet.add("One");
Iterator<String> it = mySet.iterator();
while(it.hasNext())
System.out.println(it.next());

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy