double d = 99.9; // Type cast double to int int i = (int) d;
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Java - How to compare Strings
String string1 = "5up"; String string2 = "5up"; // false string1 == string2; // true string1.equals(string2);
Java - Interface
public interface Animal { public void makeASound(); } public class Dog implements Animal { public void makeASound() { System.out.print("bark"); } public static void main(String args[]){ Dog dog = new Dog(); dog.makeASound(); } }
Result:
bark
Java - Inheritance
public class Animal { public String animalSound; public Animal(String aSound) { animalSound = aSound; } public void makeASound() { System.out.println(animalSound); } } public class Dog extends Animal { public Dog(String aSound) { super(aSound); } public static void main(String args[]) { Dog dog = new Dog("bark"); dog.makeASound(); } }
Result:
bark
Java - Convert long to int
long l = 1; int i = (int) l;
To do it safely:
public static int convertLongToInt(long l) { if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { throw new IllegalArgumentException ("Invalid value for: " + l); } return (int) l; }
Java - For Loop
for(int i = 1; i < 6; i++) { System.out.println(i); }Will produce:
1
2
3
4
5
Enhanced for loop:
List<Integer> list = new ArrayList(); list.add(1); list.add(2); list.add(3); for (Integer i : list) { System.out.println(i); }
Will produce:
1
2
3
Java - List, ArrayList
List list = new ArrayList(); // add object to the list String s = "Hello World!"; list.add(s);
Java - Convert Date to String
String stringDate = ""; DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); Date date = new Date(); stringDate = dateFormat.format(date);
Java - Convert String to Date
DateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String ddMMyyyy = "20130128"; Date date = null; date = (Date) formatter.parse(ddMMyyyy);
Or
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dd_MM_yyyy = "2013-01-28"; Date date = null; date = (Date) formatter.parse(dd_MM_yyyy);
Java - What is Java?
Java is a object oriented programming language developed by Sun Microsystems. It is platform independent which means it can run in almost any kind of computer with Java Runtime Environment (JRE) installed.
Java - Convert int to String
int i = 3; String s = ""; s = Integer.toString(i);
or
s = String.valueOf(i);
What is Apache Camel?
I recently had an opportunity to get to know Apache Camel and I have to say I'm impressed by how powerful and easy it is to use. In this first post of my blog I'm going to explain what Camel is and show a basic example of how to use it.
Apache Camel is an Open Source Java Framework based on Enterprise Integration Patterns. If you don't know what Enterprise Integration Patterns are, I suggest you to read about it first.
EIPs offer solutions for Enterprise Integration problems. Patterns use messages which are transported from component to component through message channels. EIPs describes how to route messages, transform messages, distribute them to different endpoints and how application connects to other.
A simple example would be to transfer csv file data to a relational database which resides in a different system.
This would be easily implemented with Apache Camel.
Basically Camel is a routing engine for messages. You define routing rules, select sources where to get the message, define what to do with the message and tell where to finally send the message.One of the most impressing things with Camel is, it can handle almost any kind of protocol and data.
You can use Java, Scala or XML to define routine rules and business logic with Camel's Domain-Specific Language (DSL). Or you can use them all at the same time.
Following route is equilavent with all three languages:
Java DSL:
Spring DSL:
Scala DSL:
Camel implements EIPs with routes, processors and endpoints.
A route starts from an endpoint and ends to an endpoint. This process is defined in a processor. A route can chain multiple EIPs so you can can create as complicated routes as you wish.
Apache Camel is an Open Source Java Framework based on Enterprise Integration Patterns. If you don't know what Enterprise Integration Patterns are, I suggest you to read about it first.
Enterprise Integration Patterns - EIPs
EIPs offer solutions for Enterprise Integration problems. Patterns use messages which are transported from component to component through message channels. EIPs describes how to route messages, transform messages, distribute them to different endpoints and how application connects to other.
A simple example would be to transfer csv file data to a relational database which resides in a different system.
This would be easily implemented with Apache Camel.
Apache Camel
Basically Camel is a routing engine for messages. You define routing rules, select sources where to get the message, define what to do with the message and tell where to finally send the message.One of the most impressing things with Camel is, it can handle almost any kind of protocol and data.
You can use Java, Scala or XML to define routine rules and business logic with Camel's Domain-Specific Language (DSL). Or you can use them all at the same time.
Following route is equilavent with all three languages:
Java DSL:
from("ftp://foo@myserver?password=secret").to("file:src/data");
Spring DSL:
<from uri="ftp://foo@myserver?password=secret"/> <to uri="file:src/data"/>
Scala DSL:
from "file:data/inbox" -> "jms:queue:order"
Camel implements EIPs with routes, processors and endpoints.
A route starts from an endpoint and ends to an endpoint. This process is defined in a processor. A route can chain multiple EIPs so you can can create as complicated routes as you wish.
Subscribe to:
Posts (Atom)