Skip to main content

Posts

In Java, how does System.out.println() work?

In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable. What is “out” in System.out.println()? We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? There are two possibilities – it could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we kn

In Java, what’s the difference between method overloading and method overriding?

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly simple to understand. Let’s start the discussion by talking more about method overloading. Overloading in Java  can  occur when two or more methods in the same class share the same name or even if a child class shares a method with the same name as one of it’s parent classes. But, in order to  actually  have overloaded methods, the methods not only have to have the same name, but there are other conditions that must be satisfied – read below to see what those conditions are. Suppose we have a class called TestClass which has 2 methods, and both methods have the  same  name – let’s say that name is “someMethod” – this would be considered to be method overloading if  at least one of these 2 things is true: 1.) The number of parameters is different for the methods 2.) The parameter types are different. How to NOT overload methods: It’s important to underst

In Java, what’s the difference between an object and a class?

The terms ‘class’ and ‘object’ are absolutely related to one another, but each term holds its own distinct meaning. The term ‘class’ refers to the actual written piece of code in which the class is defined. A class is a static piece of code that consists of attributes which don’t change during the execution of a program – like the method definitions within a class. An object is an instance of a class The term ‘object’, however, refers to an actual  instance  of a class. Every object must belong to a class. Objects are created and eventually destroyed – so they only live in the program for a limited time. While objects are ‘living’ their properties may also be changed signficantly. An example will help clarify what we’ve said. Suppose we have a class called ‘Animal’. All Animals have bodies and brains. So, this general ‘template’ of an Animal does not change. An instance of the Animal class would be a  specific  animal – like a lion, a cat, or a zebra. These instances of t

What’s the difference between an interface and an abstract class in Java?

A class must be declared  abstract  when it has one or more abstract  methods . A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do. When to use abstract methods in Java? Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below: /* the Figure class must be declared as abstract because it contains an abstract method */ public abstract class Figure { /* because this is an abstract method the body will be blank */ public abstract float getArea(); } public class Circle extends Figure { private float radius; public float getArea() { return (3.14 * (radius * 2)); } } public class Rectangle extends Figure { private float length, width; public float getArea(Figure other) { return length * width; } } In the Figure class above, we have an abstract method called getAre

Basic Hello World Program in Java

When we consider a Java program it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and instance variables mean. Object -  Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class. Class -  A class can be defined as a template/ blue print that describes the behaviors/states that object of its type support. Methods -  A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed. Instance Variables -  Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables. First Java Program: Let us look at a simple code that would print the words  Hello World . public class MyFi

How to delete first preceding element

Here is the html from which I want to remove the div element 'div' by clicking the link. <div> <a href="javascript:void(0)" class="link">My link</a> </div> To access   the preceding element I will use the .parents() method of jQuery and then call  the method .remove() to remove the 'div' element. Here is the code... $('a.link').click(function() { $(this).parents('div:first').remove(); });

HTML template using bootstrap

With a brief intro into the contents out of the way, we can focus on putting Bootstrap to use. To do that, we'll utilize a basic HTML template that includes everything we mentioned in the File structure . Now, here's a look at a  typical HTML file : <!DOCTYPE html> <html> <head> <title> Bootstrap 101 Template </title> <meta name = "viewport" content = "width=device-width, initial-scale=1.0" > </head> <body> <h1> Hello, world! </h1> <script src = "http://code.jquery.com/jquery.js" ></script> </body> </html> To make this  a Bootstrapped template , just include the appropriate CSS and JS files: <!DOCTYPE html> <html> <head> <title> Bootstrap 101 Template </title> <meta name = "viewport" content = "width=device-width, initial-scale=1.0" > <!--

HTTP Status Code 501 - Not Implemented

501 Error Code Explained The server either does not recognise the request method, or it lacks the ability to fulfill the request. Why it occurs The error occurs when the Web server does not understand or does not support the HTTP method it finds in the HTTP data stream sent to it by the client. The method in the request HTTP data stream should be one of the following GET, OPTIONS, HEAD, POST, PUT, DELETE, TRACE and CONNECT as defined by the HTTP protocol Or the method may be valid but not actually supported by the Web server. Fixing 501 error code The client should specify a valid request type. Even after that if the Web server is responding incorrectly, then the web server simply needs to be upgraded to fix the issue. If you are registered with 100pulse, your site will be monitored and you will be intimated by an email or a short message service when 501 status code error occurs.