Skip to content

Instantly share code, notes, and snippets.

View hakaneroztekin's full-sized avatar

Hakan Eroztekin hakaneroztekin

View GitHub Profile
Hello world :)
@hakaneroztekin
hakaneroztekin / variable-types.java
Last active September 10, 2019 11:19
Java Variables Types
class MyClass{
private Integer instanceVariable; // Instance variable
private static Integer classVariable = 5; // Class variable
public void myMethod(){
String localVariable // Local variable
}
}
class Desk {
private String model;
private String material;
public Desk() { // Default constructor
model = "wd001";
material = "wooden";
}
public Desk(String model, String material){ // Parametrized constructor
class TestingPassByValue() {
public static void main(String ... args){
// myFavoriteHobby is Playing billard
Hobby myFavoriteHobby = new Hobby("Playing billard");
// hobbyBackup is Playing billard
Hobby hobbyBackup = myFavoriteHobby;
// passing the value. Simply, you can think it
// like passing the value of the pointer (e.g. 42)
hobbyGame(myFavoriteHobby);
class Employee {
String name;
Integer salary;
public Employee() {
name = "Daniel";
salary = 120000;
}
public void printDetails(){
@Entity // the class will be stored in the DB
class HighSchool {
@Id // Id annotation
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto generated value
@Column(name = "id") // We can specify the column name (optional)
private Long id;
@Column(name = "name")
private Long name;
// In the example above, the userLister is not initialized
class DependencyProblem {
private UserLister userLister;
// gets users from DB and renders to the view (just a trivial example)
public void render() {
List<User> users = userLister.getUsers();
view.render(users);
}
}
public class StreamIntermediateOperations {
// map: The map method is used to map the items
// in the collection to other objects according to the Function passed as argument.
List number = Arrays.asList(2,3,4,5);
List square = number.stream().map(x->x*x).collect(Collectors.toList());
// filter: The filter method is used
// to select elements as per the Predicate passed as argument.
List names = Arrays.asList("Reflection","Collection","Stream");
List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
public class StreamTerminalOperations {
// collect: The collect method is used to return
// the result of the intermediate operations performed on the stream.
List number = Arrays.asList(2,3,4,5,3);
Set square = number.stream().map(x->x*x).collect(Collectors.toSet());
//forEach: The forEach method is used to
// iterate through every element of the stream.
List number = Arrays.asList(2,3,4,5);
number.stream().map(x->x*x).forEach(y->System.out.println(y));
// index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>User Form</title>
</h:head>
<h:body>
<h:outputText value="Hello world!"/>