Tutorial by Examples: at

Server: Start, and wait for incoming connections //Open a listening "ServerSocket" on port 1234. ServerSocket serverSocket = new ServerSocket(1234); while (true) { // Wait for a client connection. // Once a client connected, we get a "Socket" object // that c...
A float is a single-precision 32-bit IEEE 754 floating point number. By default, decimals are interpreted as doubles. To create a float, simply append an f to the decimal literal. double doubleExample = 0.5; // without 'f' after digits = double float floatExample = 0.5f; // with 'f' aft...
Once we've gotten the Connection, we will mostly use it to create Statement objects. Statements represent a single SQL transaction; they are used to execute a query, and retrieve the results (if any). Let's look at some examples: public void useConnection() throws SQLException{ Connection ...
An enum can contain a method, just like any class. To see how this works, we'll declare an enum like this: public enum Direction { NORTH, SOUTH, EAST, WEST; } Let's have a method that returns the enum in the opposite direction: public enum Direction { NORTH, SOUTH, EAST, WEST; ...
The Standard Edition of Java comes with some annotations predefined. You do not need to define them by yourself and you can use them immediately. They allow the compiler to enable some fundamental checking of methods, classes and code. @Override This annotation applies to a method and says that th...
The switch statement is Java's multi-way branch statement. It is used to take the place of long if-else if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined. There are three critical components to the switch...
Paginations in JavaFX use a callback to get the pages used in the animation. Pagination p = new Pagination(); p.setPageFactory(param -> new Button(param.toString())); This creates an infinite list of buttons numbed 0.. since the zero arg constructor creates an infinite pagination. setPageFac...
ArrayList<String> images = new ArrayList<>(); images.add("some\\cool\\image"); images.add("some\\other\\cool\\image"); images.add("some\\cooler\\image"); Pagination p = new Pagination(3); p.setPageFactory(n -> new ImageView(images.get(n))); Note...
Java's Reflection API allows the programmer to perform various checks and operations on class fields, methods and annotations during runtime. However, in order for an annotation to be at all visible at runtime, the RetentionPolicy must be changed to RUNTIME, as demonstrated in the example below: @i...
Date date = new Date(); System.out.println(date); // Thu Feb 25 05:03:59 IST 2016 Here this Date object contains the current date and time when this object was created. Calendar calendar = Calendar.getInstance(); calendar.set(90, Calendar.DECEMBER, 11); Date myBirthDate = calendar.getTime(); ...
Calendar, Date, and LocalDate Java SE 8 before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date(); final Calendar calendar = Calendar.getInstance(); calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date birthdate = calendar.getTime(); ...
format() from SimpleDateFormat class helps to convert a Date object into certain format String object by using the supplied pattern string. Date today = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //pattern is specified here System.out.println(date...
parse() from SimpleDateFormat class helps to convert a String pattern into a Date object. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); String dateStr = "02/25/2016"; // input String Date date = dateFormat.parse(dateStr); System.out.println(date.getYe...
Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar. It's important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as...
When desired XML format differs from Java object model, an XmlAdapter implementation can be used to transform model object into xml-format object and vice versa. This example demonstrates how to put a field's value into an attribute of an element with field's name. public class XmlAdapterExample { ...
You should be careful when comparing floating-point values (float or double) using relational operators: ==, !=, < and so on. These operators give results according to the binary representations of the floating point values. For example: public class CompareTest { public static void main...
Button button = new Button("I'm here..."); Timeline t = new Timeline( new KeyFrame(Duration.seconds(0), new KeyValue(button.translateXProperty(), 0)), new KeyFrame(Duration.seconds(2), new KeyValue(button.translateXProperty(), 80)) ); t.setAutoReverse(true); t.setCy...
Annotation @XmlAccessorType determines whether fields/properties will be automatically serialized to XML. Note, that field and method annotations @XmlElement, @XmlAttribute or @XmlTransient take precedence over the default settings. public class XmlAccessTypeExample { @XmlAccessorType(XmlAccessT...
Annotations @XmlElement, @XmlAttribute or @XmlTransient and other in package javax.xml.bind.annotation allow the programmer to specify which and how marked fields or properties should be serialized. @XmlAccessorType(XmlAccessType.NONE) // we want no automatic field/property marshalling public cla...
The + symbol can mean three distinct operators in Java: If there is no operand before the +, then it is the unary Plus operator. If there are two operands, and they are both numeric. then it is the binary Addition operator. If there are two operands, and at least one of them is a String, then i...

Page 6 of 442